It looks like you want to add a CloudFront Cache Invalidation step to your Serverless Framework
configuration. This can be done using a Serverless Framework plugin called serverless-
cloudfront-invalidate.
To achieve this, follow these steps:
. Install the Plugin:
Open your terminal and navigate to the root directory of your Serverless project. Then run the
following command:
npm install --save serverless-cloudfront-invalidate
This will install the serverless-cloudfront-invalidate plugin and add it to your
package.json.
. Configure the Plugin:
In your serverless.yml file, add the serverless-cloudfront-invalidate plugin to your
plugins section. This section might already exist, or you can create it if it doesn't.
plugins:
- serverless-cloudfront-invalidate
. Define CloudFront Invalidation Settings:
Add a section under custom in your serverless.yml file to configure the plugin:
custom:
cloudfrontInvalidate:
distributionId: YOUR_CLOUDFRONT_DISTRIBUTION_ID
paths:
- /path/to/invalidate/*
Replace YOUR_CLOUDFRONT_DISTRIBUTION_ID with the actual CloudFront Distribution
ID you want to invalidate.
You can specify multiple paths to invalidate.
. Invalidate Cache After Deployment:
To automatically invalidate the CloudFront cache after a deployment, you can use the
after:deploy lifecycle event. Add the following to your serverless.yml:
PROFESSEUR : M.DA ROS ✦ 1 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL
custom:
cloudfrontInvalidate:
distributionId: YOUR_CLOUDFRONT_DISTRIBUTION_ID
paths:
- /path/to/invalidate/*
...
# Add the following section to your serverless.yml
# This will run the cache invalidation after each deployment.
hooks:
after:deploy:invalidateCache:
- serverless-cloudfront-invalidate
. Deploy Your Serverless Service:
Now, whenever you deploy your Serverless service, the serverless-cloudfront-
invalidate plugin will be triggered after the deployment process, and it will invalidate the
specified paths in your CloudFront distribution.
Remember to replace YOUR_CLOUDFRONT_DISTRIBUTION_ID with your actual CloudFront distribution
ID, and adjust the paths to match the resources you want to invalidate.
After making these changes, you can deploy your Serverless service with the added CloudFront cache
invalidation step.
I'm unable to directly modify files or send files to you. However, I can provide you with the updated
serverless.yml content that includes the CloudFront cache invalidation step.
service: YOUR_SERVICE_NAME
provider:
name: aws
runtime: nodejs14.x
region: YOUR_AWS_REGION
plugins:
- serverless-cloudfront-invalidate
custom:
cloudfrontInvalidate:
distributionId: YOUR_CLOUDFRONT_DISTRIBUTION_ID
paths:
- /path/to/invalidate/*
functions:
# Define your functions here
resources:
Resources:
CloudFront:
PROFESSEUR : M.DA ROS ✦ 2 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
ViewerCertificate:
AcmCertificateArn: ${env:ACM_CERTIFICATE_ARN}
SslSupportMethod: sni-only
Enabled: true
HttpVersion: http2
DefaultRootObject: index.html
Aliases:
- ${self:provider.environment.DOMAINE_NAME}
Origins:
- DomainName:
Fn::Join:
- "."
- - Ref: PublicSiteBucket
- Fn::FindInMap:
- RegionMap
- Ref: "AWS::Region"
- websiteendpoint
Id: S3-${self:provider.environment.PUBLIC_SITE_BUCKET}
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: http-only
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
DefaultCacheBehavior:
ViewerProtocolPolicy: redirect-to-https
TargetOriginId:
S3-${self:provider.environment.PUBLIC_SITE_BUCKET}
AllowedMethods:
- GET
- HEAD
Compress: true
ForwardedValues:
QueryString: false
Please make sure to replace the following placeholders with your actual values:
YOUR_SERVICE_NAME: Replace this with the name of your service.
YOUR_AWS_REGION: Replace with the AWS region where you want to deploy.
YOUR_CLOUDFRONT_DISTRIBUTION_ID: Replace with your actual CloudFront Distribution ID.
/path/to/invalidate/*: Replace with the paths you want to invalidate.
After making these replacements, save the file and then deploy your Serverless service using the
serverless deploy command. This updated configuration should now include the CloudFront cache
invalidation step.
PROFESSEUR : M.DA ROS ✦ 3 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL
To get the CloudFront Distribution ID, you can follow these steps:
. Using AWS Management Console:
Go to the AWS Management Console.
Navigate to the CloudFront service.
In the CloudFront dashboard, you'll see a list of your distributions. Click on the one you
want to get the ID for.
In the distribution details page, you'll find the Distribution ID listed.
. Using AWS CLI:
If you have the AWS CLI installed and configured on your system, you can use the following
command:
aws cloudfront list-distributions --query
"DistributionList.Items[].{ID:Id, DomainName:DomainName}"
This will list all your CloudFront distributions along with their IDs.
Please note that the Distribution ID is a unique identifier for a CloudFront distribution and will look
something like E123456789ABCD. Be sure to replace YOUR_CLOUDFRONT_DISTRIBUTION_ID in your
Serverless YAML file with the actual ID you obtain.
If you're using a Serverless Framework template that already includes a CloudFront distribution, you can
retrieve the CloudFront Distribution ID from the template itself.
Given your provided Serverless Framework template, the CloudFront Distribution resource is
defined under Resources with the key CloudFront. To extract the Distribution ID, you can use
${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}. Here's
how you can incorporate it into your template:
service: YOUR_SERVICE_NAME
provider:
name: aws
runtime: nodejs14.x
region: YOUR_AWS_REGION
plugins:
- serverless-cloudfront-invalidate
custom:
cloudfrontInvalidate:
distributionId:
${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}
paths:
- /path/to/invalidate/*
PROFESSEUR : M.DA ROS ✦ 4 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL
functions:
# Define your functions here
resources:
Resources:
CloudFront:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
# ... other CloudFront properties ...
Replace YOUR_SERVICE_NAME, YOUR_AWS_REGION, and /path/to/invalidate/ If you're
using a Serverless Framework template that already includes a CloudFront
distribution, you can retrieve the CloudFront Distribution ID` from the template itself.
Given your provided Serverless Framework template, the CloudFront Distribution resource is
defined under Resources with the key CloudFront. To extract the Distribution ID, you can use
${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}. Here's
how you can incorporate it into your template:
service: YOUR_SERVICE_NAME
provider:
name: aws
runtime: nodejs14.x
region: YOUR_AWS_REGION
plugins:
- serverless-cloudfront-invalidate
custom:
cloudfrontInvalidate:
distributionId:
${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}
paths:
- /path/to/invalidate/*
functions:
# Define your functions here
resources:
Resources:
CloudFront:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
# ... other CloudFront properties ...
PROFESSEUR : M.DA ROS ✦ 5 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL
Replace YOUR_SERVICE_NAME, YOUR_AWS_REGION, and /path/to/invalidate/* with your actual
values.
The ${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}
expression will retrieve the Distribution ID from the CloudFront resource.
` with your actual values.
The ${self:resources.Resources.CloudFront.Properties.DistributionConfig.Id}
expression will retrieve the Distribution ID from the CloudFront resource.
PROFESSEUR : M.DA ROS ✦ 6 / 6 ✦ BTS SIO BORDEAUX - LYCÉE GUSTAVE EIFFEL