Deploying Static Website to S3 with AWS CodeBuild
- By : Mydatahack
- Category : AWS, Infrastructure
- Tags: AWS, CodeDeploy, FED, S3
There are a few AWS devops services, CodeDeploy, CodeBuild and CodePipeline. They have similar name and it’s confusing at first. CodeBuild is the service to create an artefact. In CodeBuild, we can install dependencies, run unit tests, create an artefact and push to the artefact repository. CodeDeploy uses the artefact and deploy the code to the environment. CodePipeline is the orchestrator.
When we want to deploy a static website or frontend widget to S3 and directly serves those static assets from there, CodeBuild itself can do all of it. In the build project, we specify the repo and use buildspec.yml to install dependencies, run tests, compile and push to S3. Then, those assets can be served from the bucket directly.
buildspec.yml defines the actions that happens in CodeBuild. Each phase, we can run Linux command. It also allows to pass the variables from the CodeBuild. It will read the buildspec file at the root of your repo.
Now, let’s see the example to push JavaScript widget to S3 bucket. Since the bucket uses CloudFront as CDN, we need to clear CloudFront cache after each deployment. It can be done with aws cli command.
The buildspec file only creates an artefact. In CodeBuild, we need to specify the Artifact upload location to the target S3 bucket.
That’s it. We don’t need to use CodeDeploy or CodePipeline to deploy frontend widget to S3 bucket. Pretty cool.