Force CloudFront distribution/file update

后端 未结 13 1197
失恋的感觉
失恋的感觉 2020-12-07 07:01

I\'m using Amazon\'s CloudFront to serve static files of my web apps.

Is there no way to tell a cloudfront distribution that it needs to refresh it\'s file or point

相关标签:
13条回答
  • 2020-12-07 07:44

    Set TTL=1 hour and replace

    http://developer.amazonwebservices.com/connect/ann.jspa?annID=655

    0 讨论(0)
  • 2020-12-07 07:49

    With the Invalidation API, it does get updated in a few of minutes.
    Check out PHP Invalidator.

    0 讨论(0)
  • 2020-12-07 07:52

    Automated update setup in 5 mins

    OK, guys. The best possible way for now to perform automatic CloudFront update (invalidation) is to create Lambda function that will be triggered every time when any file is uploaded to S3 bucket (a new one or rewritten).

    Even if you never used lambda functions before, it is really easy -- just follow my step-by-step instructions and it will take just 5 mins:

    Step 1

    Go to https://console.aws.amazon.com/lambda/home and click Create a lambda function

    Step 2

    Click on Blank Function (custom)

    Step 3

    Click on empty (stroked) box and select S3 from combo

    Step 4

    Select your Bucket (same as for CloudFront distribution)

    Step 5

    Set an Event Type to "Object Created (All)"

    Step 6

    Set Prefix and Suffix or leave it empty if you don't know what it is.

    Step 7

    Check Enable trigger checkbox and click Next

    Step 8

    Name your function (something like: YourBucketNameS3ToCloudFrontOnCreateAll)

    Step 9

    Select Python 2.7 (or later) as Runtime

    Step 10

    Paste following code instead of default python code:

    from __future__ import print_function
    
    import boto3
    import time
    
    def lambda_handler(event, context):
        for items in event["Records"]:
            path = "/" + items["s3"]["object"]["key"]
            print(path)
            client = boto3.client('cloudfront')
            invalidation = client.create_invalidation(DistributionId='_YOUR_DISTRIBUTION_ID_',
                InvalidationBatch={
                'Paths': {
                'Quantity': 1,
                'Items': [path]
                },
                'CallerReference': str(time.time())
                })
    

    Step 11

    Open https://console.aws.amazon.com/cloudfront/home in a new browser tab and copy your CloudFront distribution ID for use in next step.

    Step 12

    Return to lambda tab and paste your distribution id instead of _YOUR_DISTRIBUTION_ID_ in the Python code. Keep surrounding quotes.

    Step 13

    Set handler: lambda_function.lambda_handler

    Step 14

    Click on the role combobox and select Create a custom role. New tab in browser will be opened.

    Step 15

    Click view policy document, click edit, click OK and replace role definition with following (as is):

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "logs:CreateLogGroup",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
          ],
          "Resource": "arn:aws:logs:*:*:*"
        },
        {
          "Effect": "Allow",
          "Action": [
              "cloudfront:CreateInvalidation"
          ],
          "Resource": [
              "*"
          ]
        }
      ]
    }
    

    Step 16

    Click allow. This will return you to a lambda. Double check that role name that you just created is selected in the Existing role combobox.

    Step 17

    Set Memory (MB) to 128 and Timeout to 5 sec.

    Step 18

    Click Next, then click Create function

    Step 19

    You are good to go! Now on, each time you will upload/reupload any file to S3, it will be evaluated in all CloudFront Edge locations.

    PS - When you are testing, make sure that your browser is loading images from CloudFront, not from local cache.

    PSS - Please note, that only first 1000 files invalidation per month are for free, each invalidation over limit cost $0.005 USD. Also additional charges for Lambda function may apply, but it is extremely cheap.

    0 讨论(0)
  • 2020-12-07 07:53

    Good news. Amazon finally added an Invalidation Feature. See the API Reference.

    This is a sample request from the API Reference:

    POST /2010-08-01/distribution/[distribution ID]/invalidation HTTP/1.0
    Host: cloudfront.amazonaws.com
    Authorization: [AWS authentication string]
    Content-Type: text/xml
    
    <InvalidationBatch>
       <Path>/image1.jpg</Path>
       <Path>/image2.jpg</Path>
       <Path>/videos/movie.flv</Path>
       <CallerReference>my-batch</CallerReference>
    </InvalidationBatch>
    
    0 讨论(0)
  • 2020-12-07 07:53

    Bucket Explorer has a UI that makes this pretty easy now. Here's how:

    Right click your bucket. Select "Manage Distributions."
    Right click your distribution. Select "Get Cloudfront invalidation list" Then select "Create" to create a new invalidation list. Select the files to invalidate, and click "Invalidate." Wait 5-15 minutes.

    0 讨论(0)
  • 2020-12-07 07:54

    current AWS CLI support invalidation in preview mode. Run the following in your console once:

    aws configure set preview.cloudfront true
    

    I deploy my web project using npm. I have the following scripts in my package.json:

    {
        "build.prod": "ng build --prod --aot",
        "aws.deploy": "aws s3 sync dist/ s3://www.mywebsite.com --delete --region us-east-1",
        "aws.invalidate": "aws cloudfront create-invalidation --distribution-id [MY_DISTRIBUTION_ID] --paths /",
        "deploy": "npm run build.prod && npm run aws.deploy && npm run aws.invalidate"
    }
    

    Having the scripts above in place you can deploy your site with:

    npm run deploy
    
    0 讨论(0)
提交回复
热议问题