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.