|
| 1 | +AWSTemplateFormatVersion: 2010-09-09 |
| 2 | +Transform: 'AWS::Serverless-2016-10-31' |
| 3 | +Description: Frontend Deploy Example |
| 4 | + |
| 5 | +Resources: |
| 6 | + TargetBucket: |
| 7 | + Type: AWS::S3::Bucket |
| 8 | + |
| 9 | + DeploymentLayer: |
| 10 | + Type: AWS::Serverless::Application |
| 11 | + Properties: |
| 12 | + Location: |
| 13 | + ApplicationId: arn:aws:serverlessrepo:us-east-1:375983427419:applications/deploy-to-s3 |
| 14 | + SemanticVersion: 1.0.0 |
| 15 | + |
| 16 | + |
| 17 | + # this function is used only during deployment, |
| 18 | + # we use the web site assets as the source of the function |
| 19 | + # tricking cloudformation to pack up the web site files |
| 20 | + # using the standard cloudformation package process |
| 21 | + SiteSource: |
| 22 | + Type: AWS::Serverless::Function |
| 23 | + Properties: |
| 24 | + Layers: |
| 25 | + # the layer contains the deployment code |
| 26 | + # so the function "source" can just contain the web assets |
| 27 | + - !GetAtt DeploymentLayer.Outputs.Arn |
| 28 | + |
| 29 | + # point to directory with the assets so cloudformation can |
| 30 | + # package and upload them |
| 31 | + CodeUri: web-site/ |
| 32 | + |
| 33 | + # really important: this will ensure that any change in |
| 34 | + # the bundled files gets deployed again. we're abusing |
| 35 | + # the custom resource pipeline here, so this will be used |
| 36 | + # to change parameters of the resource and re-trigger it |
| 37 | + AutoPublishAlias: live |
| 38 | + |
| 39 | + # the following two lines are required to make the layer work |
| 40 | + Runtime: python3.6 |
| 41 | + Handler: deployer.resource_handler |
| 42 | + |
| 43 | + # set the timeout to something reasonable depending on |
| 44 | + # how long it takes to upload your assets to S3 |
| 45 | + Timeout: 600 |
| 46 | + |
| 47 | + # give the function access to the bucket where it |
| 48 | + # will upload the assets |
| 49 | + Policies: |
| 50 | + - S3FullAccessPolicy: |
| 51 | + BucketName: !Ref TargetBucket |
| 52 | + |
| 53 | + # This is a custom resource that |
| 54 | + # will trigger the function during deployment |
| 55 | + DeploymentResource: |
| 56 | + Type: AWS::CloudFormation::CustomResource |
| 57 | + Properties: |
| 58 | + |
| 59 | + # the following two lines are required to |
| 60 | + # ensure that cloudformation will trigger the |
| 61 | + # resource every time you change the bundled files |
| 62 | + ServiceToken: !GetAtt SiteSource.Arn |
| 63 | + Version: !Ref "SiteSource.Version" |
| 64 | + |
| 65 | + # tell the deployer where to upload the files |
| 66 | + TargetBucket: !Ref TargetBucket |
| 67 | + |
| 68 | + Substitutions: |
| 69 | + FilePattern: "*.html" |
| 70 | + Values: |
| 71 | + APP_NAME: 'Example Application' |
| 72 | + STACK_ID: !Ref AWS::StackId |
| 73 | + |
| 74 | + # Choose the ACL and caching policies |
| 75 | + # eg, for directly accessible web site |
| 76 | + # use public-read and 10 minutes caching |
| 77 | + Acl: 'public-read' |
| 78 | + CacheControlMaxAge: 600 |
| 79 | + |
| 80 | + |
| 81 | +Outputs: |
| 82 | + DestinationBucket: |
| 83 | + Value: !Ref TargetBucket |
0 commit comments