What is @aws-sdk/client-cloudfront?
@aws-sdk/client-cloudfront is a part of the AWS SDK for JavaScript, which allows developers to interact with Amazon CloudFront, a content delivery network (CDN) service. This package provides a variety of functionalities to manage CloudFront distributions, invalidations, and other related resources.
What are @aws-sdk/client-cloudfront's main functionalities?
Create CloudFront Distribution
This feature allows you to create a new CloudFront distribution. The code sample demonstrates how to set up a distribution with a custom origin and default cache behavior.
const { CloudFrontClient, CreateDistributionCommand } = require('@aws-sdk/client-cloudfront');
const client = new CloudFrontClient({ region: 'us-east-1' });
const params = {
DistributionConfig: {
CallerReference: 'unique-string',
Origins: {
Quantity: 1,
Items: [
{
Id: 'origin-1',
DomainName: 'example.com',
CustomOriginConfig: {
HTTPPort: 80,
HTTPSPort: 443,
OriginProtocolPolicy: 'http-only'
}
}
]
},
DefaultCacheBehavior: {
TargetOriginId: 'origin-1',
ViewerProtocolPolicy: 'allow-all',
ForwardedValues: {
QueryString: false,
Cookies: {
Forward: 'none'
}
},
TrustedSigners: {
Enabled: false,
Quantity: 0
}
},
Enabled: true
}
};
const run = async () => {
try {
const data = await client.send(new CreateDistributionCommand(params));
console.log('Distribution created:', data);
} catch (err) {
console.error('Error creating distribution:', err);
}
};
run();
List CloudFront Distributions
This feature allows you to list all CloudFront distributions in your AWS account. The code sample demonstrates how to retrieve and log the list of distributions.
const { CloudFrontClient, ListDistributionsCommand } = require('@aws-sdk/client-cloudfront');
const client = new CloudFrontClient({ region: 'us-east-1' });
const run = async () => {
try {
const data = await client.send(new ListDistributionsCommand({}));
console.log('Distributions:', data.DistributionList.Items);
} catch (err) {
console.error('Error listing distributions:', err);
}
};
run();
Create Invalidation
This feature allows you to create an invalidation for a CloudFront distribution. The code sample demonstrates how to invalidate specific paths in a distribution.
const { CloudFrontClient, CreateInvalidationCommand } = require('@aws-sdk/client-cloudfront');
const client = new CloudFrontClient({ region: 'us-east-1' });
const params = {
DistributionId: 'EXAMPLE_DIST_ID',
InvalidationBatch: {
CallerReference: 'unique-string',
Paths: {
Quantity: 1,
Items: ['/path/to/invalidate']
}
}
};
const run = async () => {
try {
const data = await client.send(new CreateInvalidationCommand(params));
console.log('Invalidation created:', data);
} catch (err) {
console.error('Error creating invalidation:', err);
}
};
run();
Other packages similar to @aws-sdk/client-cloudfront
cloudflare
The 'cloudflare' npm package allows developers to interact with Cloudflare's CDN and other services. It provides functionalities similar to @aws-sdk/client-cloudfront, such as managing DNS records, creating and managing zones, and purging cache. However, it is specific to Cloudflare's infrastructure.
fastly
The 'fastly' npm package provides an interface to interact with Fastly's CDN services. It offers functionalities like creating and managing services, purging cache, and configuring domains. This package is specific to Fastly's CDN platform and offers similar capabilities to @aws-sdk/client-cloudfront but within the Fastly ecosystem.
akamai-edgegrid
The 'akamai-edgegrid' npm package allows developers to interact with Akamai's CDN and other services. It provides functionalities for managing edge configurations, purging cache, and monitoring traffic. This package is tailored for Akamai's platform and offers similar CDN management capabilities as @aws-sdk/client-cloudfront.
@aws-sdk/client-cloudfront
Description
AWS SDK for JavaScript CloudFront Client for Node.js, Browser and React Native.
Amazon CloudFront
This is the Amazon CloudFront API Reference. This guide is for developers
who need detailed information about CloudFront API actions, data types, and errors. For
detailed information about CloudFront features, see the
Amazon CloudFront Developer Guide.
Installing
To install this package, simply type add or install @aws-sdk/client-cloudfront
using your favorite package manager:
npm install @aws-sdk/client-cloudfront
yarn add @aws-sdk/client-cloudfront
pnpm add @aws-sdk/client-cloudfront
Getting Started
Import
The AWS SDK is modulized by clients and commands.
To send a request, you only need to import the CloudFrontClient
and
the commands you need, for example ListCachePoliciesCommand
:
const { CloudFrontClient, ListCachePoliciesCommand } = require("@aws-sdk/client-cloudfront");
import { CloudFrontClient, ListCachePoliciesCommand } from "@aws-sdk/client-cloudfront";
Usage
To send a request, you:
- Initiate client with configuration (e.g. credentials, region).
- Initiate command with input parameters.
- Call
send
operation on client with command object as input. - If you are using a custom http handler, you may call
destroy()
to close open connections.
const client = new CloudFrontClient({ region: "REGION" });
const params = {
};
const command = new ListCachePoliciesCommand(params);
Async/await
We recommend using await
operator to wait for the promise returned by send operation as follows:
try {
const data = await client.send(command);
} catch (error) {
} finally {
}
Async-await is clean, concise, intuitive, easy to debug and has better error handling
as compared to using Promise chains or callbacks.
Promises
You can also use Promise chaining
to execute send operation.
client.send(command).then(
(data) => {
},
(error) => {
}
);
Promises can also be called using .catch()
and .finally()
as follows:
client
.send(command)
.then((data) => {
})
.catch((error) => {
})
.finally(() => {
});
Callbacks
We do not recommend using callbacks because of callback hell,
but they are supported by the send operation.
client.send(command, (err, data) => {
});
v2 compatible style
The client can also send requests using v2 compatible style.
However, it results in a bigger bundle size and may be dropped in next major version. More details in the blog post
on modular packages in AWS SDK for JavaScript
import * as AWS from "@aws-sdk/client-cloudfront";
const client = new AWS.CloudFront({ region: "REGION" });
try {
const data = await client.listCachePolicies(params);
} catch (error) {
}
client
.listCachePolicies(params)
.then((data) => {
})
.catch((error) => {
});
client.listCachePolicies(params, (err, data) => {
});
Troubleshooting
When the service returns an exception, the error will include the exception information,
as well as response metadata (e.g. request id).
try {
const data = await client.send(command);
} catch (error) {
const { requestId, cfId, extendedRequestId } = error.$metadata;
console.log({ requestId, cfId, extendedRequestId });
}
Getting Help
Please use these community resources for getting help.
We use the GitHub issues for tracking bugs and feature requests, but have limited bandwidth to address them.
To test your universal JavaScript code in Node.js, browser and react-native environments,
visit our code samples repo.
Contributing
This client code is generated automatically. Any modifications will be overwritten the next time the @aws-sdk/client-cloudfront
package is updated.
To contribute to client you can check our generate clients scripts.
License
This SDK is distributed under the
Apache License, Version 2.0,
see LICENSE for more information.
Client Commands (Operations List)
AssociateAlias
Command API Reference / Input / Output
CopyDistribution
Command API Reference / Input / Output
CreateAnycastIpList
Command API Reference / Input / Output
CreateCachePolicy
Command API Reference / Input / Output
CreateCloudFrontOriginAccessIdentity
Command API Reference / Input / Output
CreateContinuousDeploymentPolicy
Command API Reference / Input / Output
CreateDistribution
Command API Reference / Input / Output
CreateDistributionWithTags
Command API Reference / Input / Output
CreateFieldLevelEncryptionConfig
Command API Reference / Input / Output
CreateFieldLevelEncryptionProfile
Command API Reference / Input / Output
CreateFunction
Command API Reference / Input / Output
CreateInvalidation
Command API Reference / Input / Output
CreateKeyGroup
Command API Reference / Input / Output
CreateKeyValueStore
Command API Reference / Input / Output
CreateMonitoringSubscription
Command API Reference / Input / Output
CreateOriginAccessControl
Command API Reference / Input / Output
CreateOriginRequestPolicy
Command API Reference / Input / Output
CreatePublicKey
Command API Reference / Input / Output
CreateRealtimeLogConfig
Command API Reference / Input / Output
CreateResponseHeadersPolicy
Command API Reference / Input / Output
CreateStreamingDistribution
Command API Reference / Input / Output
CreateStreamingDistributionWithTags
Command API Reference / Input / Output
CreateVpcOrigin
Command API Reference / Input / Output
DeleteAnycastIpList
Command API Reference / Input / Output
DeleteCachePolicy
Command API Reference / Input / Output
DeleteCloudFrontOriginAccessIdentity
Command API Reference / Input / Output
DeleteContinuousDeploymentPolicy
Command API Reference / Input / Output
DeleteDistribution
Command API Reference / Input / Output
DeleteFieldLevelEncryptionConfig
Command API Reference / Input / Output
DeleteFieldLevelEncryptionProfile
Command API Reference / Input / Output
DeleteFunction
Command API Reference / Input / Output
DeleteKeyGroup
Command API Reference / Input / Output
DeleteKeyValueStore
Command API Reference / Input / Output
DeleteMonitoringSubscription
Command API Reference / Input / Output
DeleteOriginAccessControl
Command API Reference / Input / Output
DeleteOriginRequestPolicy
Command API Reference / Input / Output
DeletePublicKey
Command API Reference / Input / Output
DeleteRealtimeLogConfig
Command API Reference / Input / Output
DeleteResponseHeadersPolicy
Command API Reference / Input / Output
DeleteStreamingDistribution
Command API Reference / Input / Output
DeleteVpcOrigin
Command API Reference / Input / Output
DescribeFunction
Command API Reference / Input / Output
DescribeKeyValueStore
Command API Reference / Input / Output
GetAnycastIpList
Command API Reference / Input / Output
GetCachePolicy
Command API Reference / Input / Output
GetCachePolicyConfig
Command API Reference / Input / Output
GetCloudFrontOriginAccessIdentity
Command API Reference / Input / Output
GetCloudFrontOriginAccessIdentityConfig
Command API Reference / Input / Output
GetContinuousDeploymentPolicy
Command API Reference / Input / Output
GetContinuousDeploymentPolicyConfig
Command API Reference / Input / Output
GetDistribution
Command API Reference / Input / Output
GetDistributionConfig
Command API Reference / Input / Output
GetFieldLevelEncryption
Command API Reference / Input / Output
GetFieldLevelEncryptionConfig
Command API Reference / Input / Output
GetFieldLevelEncryptionProfile
Command API Reference / Input / Output
GetFieldLevelEncryptionProfileConfig
Command API Reference / Input / Output
GetFunction
Command API Reference / Input / Output
GetInvalidation
Command API Reference / Input / Output
GetKeyGroup
Command API Reference / Input / Output
GetKeyGroupConfig
Command API Reference / Input / Output
GetMonitoringSubscription
Command API Reference / Input / Output
GetOriginAccessControl
Command API Reference / Input / Output
GetOriginAccessControlConfig
Command API Reference / Input / Output
GetOriginRequestPolicy
Command API Reference / Input / Output
GetOriginRequestPolicyConfig
Command API Reference / Input / Output
GetPublicKey
Command API Reference / Input / Output
GetPublicKeyConfig
Command API Reference / Input / Output
GetRealtimeLogConfig
Command API Reference / Input / Output
GetResponseHeadersPolicy
Command API Reference / Input / Output
GetResponseHeadersPolicyConfig
Command API Reference / Input / Output
GetStreamingDistribution
Command API Reference / Input / Output
GetStreamingDistributionConfig
Command API Reference / Input / Output
GetVpcOrigin
Command API Reference / Input / Output
ListAnycastIpLists
Command API Reference / Input / Output
ListCachePolicies
Command API Reference / Input / Output
ListCloudFrontOriginAccessIdentities
Command API Reference / Input / Output
ListConflictingAliases
Command API Reference / Input / Output
ListContinuousDeploymentPolicies
Command API Reference / Input / Output
ListDistributions
Command API Reference / Input / Output
ListDistributionsByAnycastIpListId
Command API Reference / Input / Output
ListDistributionsByCachePolicyId
Command API Reference / Input / Output
ListDistributionsByKeyGroup
Command API Reference / Input / Output
ListDistributionsByOriginRequestPolicyId
Command API Reference / Input / Output
ListDistributionsByRealtimeLogConfig
Command API Reference / Input / Output
ListDistributionsByResponseHeadersPolicyId
Command API Reference / Input / Output
ListDistributionsByVpcOriginId
Command API Reference / Input / Output
ListDistributionsByWebACLId
Command API Reference / Input / Output
ListFieldLevelEncryptionConfigs
Command API Reference / Input / Output
ListFieldLevelEncryptionProfiles
Command API Reference / Input / Output
ListFunctions
Command API Reference / Input / Output
ListInvalidations
Command API Reference / Input / Output
ListKeyGroups
Command API Reference / Input / Output
ListKeyValueStores
Command API Reference / Input / Output
ListOriginAccessControls
Command API Reference / Input / Output
ListOriginRequestPolicies
Command API Reference / Input / Output
ListPublicKeys
Command API Reference / Input / Output
ListRealtimeLogConfigs
Command API Reference / Input / Output
ListResponseHeadersPolicies
Command API Reference / Input / Output
ListStreamingDistributions
Command API Reference / Input / Output
ListTagsForResource
Command API Reference / Input / Output
ListVpcOrigins
Command API Reference / Input / Output
PublishFunction
Command API Reference / Input / Output
TagResource
Command API Reference / Input / Output
TestFunction
Command API Reference / Input / Output
UntagResource
Command API Reference / Input / Output
UpdateCachePolicy
Command API Reference / Input / Output
UpdateCloudFrontOriginAccessIdentity
Command API Reference / Input / Output
UpdateContinuousDeploymentPolicy
Command API Reference / Input / Output
UpdateDistribution
Command API Reference / Input / Output
UpdateDistributionWithStagingConfig
Command API Reference / Input / Output
UpdateFieldLevelEncryptionConfig
Command API Reference / Input / Output
UpdateFieldLevelEncryptionProfile
Command API Reference / Input / Output
UpdateFunction
Command API Reference / Input / Output
UpdateKeyGroup
Command API Reference / Input / Output
UpdateKeyValueStore
Command API Reference / Input / Output
UpdateOriginAccessControl
Command API Reference / Input / Output
UpdateOriginRequestPolicy
Command API Reference / Input / Output
UpdatePublicKey
Command API Reference / Input / Output
UpdateRealtimeLogConfig
Command API Reference / Input / Output
UpdateResponseHeadersPolicy
Command API Reference / Input / Output
UpdateStreamingDistribution
Command API Reference / Input / Output
UpdateVpcOrigin
Command API Reference / Input / Output