Some time in the past I wrote how one can host your individual single page application on S3. However how will you get your utility on the S3 bucket? There are a few choices right here, you would add it by hand? However we each know that isn’t the true resolution right here. No we wish to automate this course of! On this weblog submit I’ll present you how one can automate this utilizing AWS CodePipeline.
The Pipeline
AWS Codepipeline makes use of totally different phases, I typically use Supply
, Construct
and Deploy
phases. In some instances I break up the Deploy stage right into a Growth, Testing, Acceptance and Manufacturing deployment (also referred to as DTAP). If you wish to know extra about how one can set this up you possibly can learn my building applications with pipelines weblog. However ultimately it’s as much as you and what is sensible to your use-case.
Once you deploy your infrastructure utilizing CloudFormation. You may make use of the outputs throughout the CodePipeline. Another choice is to make use of a naming conference. I like to make use of the outputs because it removes the necessity to outline a reputation upfront. Making it extra strong if you re-use snippets or deploy your infrastructure greater than as soon as.
Outputs:
ApplicationBucketName:
Worth: !Ref ApplicationBucket
The subsequent factor you must outline is a namespace on the motion that deploys your infrastructure.
- Title: ExecuteChangeSet
Area: eu-west-1
RunOrder: 2
RoleArn: !Sub arn:aws:iam::${DevelopmentAccountId}:position/cross-account-role
Namespace: DevelopmentVariables
ActionTypeId:
Class: Deploy
Proprietor: AWS
Supplier: CloudFormation
Model: "1"
Configuration:
ActionMode: CHANGE_SET_EXECUTE
RoleArn: !Sub arn:aws:iam::${DevelopmentAccountId}:position/cloudformation-execution-role
StackName: !Sub ${ProjectName}-development
ChangeSetName: !Sub ${ProjectName}-development-ChangeSet
By default CodePipeline will load the outputs within the given namespace. On this instance that’s DevelopmentVariables
, so the ApplicationBucketName
is obtainable as: #{DevelopmentVariables.ApplicationBucketName}
.
Deploy to S3
AWS supplies a S3 Deploy action you need to use this motion to deploy an artifact in your pipeline to S3. You possibly can create this artifact in a CodeBuild Challenge or you need to use the supply artifact.
I’m utilizing a cross account deployment technique. Because of this I want to permit my cross-account-role to permit uploads to the S3 buckets. I’m utilizing a BucketPolicy for this:
- Sid: AllowPipelineToUpload
Impact: Permit
Motion: s3:PutObject
Principal:
AWS: !Sub arn:aws:iam::${AWS::AccountId}:position/cross-account-role
Useful resource: !Sub ${ApplicationBucket.Arn}/*
Be aware that the position and the bucket live in the identical account. The pipeline lives in my construct/deployment account. So within the pipeline we have to configure the add to S3:
- Title: Eire-Uploadapplication
Area: eu-west-1
RunOrder: 3
RoleArn: !Sub arn:aws:iam::${DevelopmentAccountId}:position/cross-account-role
InputArtifacts:
- Title: utility
ActionTypeId:
Class: Deploy
Proprietor: AWS
Supplier: S3
Model: "1"
Configuration:
BucketName: "#{DevelopmentVariables.ApplicationBucketName}"
Extract: true
CacheControl: max-age=0, no-cache, no-store, must-revalidate
On this instance I’ll use my artifact referred to as utility and extract the content material within the S3 bucket. It can assume the position that we specify as RoleArn
to carry out the add. I can even set the CacheControl
in order that CloudFront is aware of that it must serve the brand new content material.
Conclusion
It’s straightforward to make use of the S3 Deploy motion to add your content material to a S3 bucket. It removes the necessity of utilizing a CodeBuild undertaking to add the content material. This can cut back price and complexity, by not sustaining an additional CodeBuild undertaking.