10
votes

I have a site in a S3 bucket, configured for web access, for which I run an aws s3 sync command every time I push on a specific git repository (I'm using Gitlab at the moment).

So if I push to stable branch, a Gitlab runner performs the npm start build command for building the site, and then aws s3 sync to synchronize to a specific bucket.

I want to migrate to CodeCommit and use pure AWS tools to do the same.


So far I was able to successfully setup the repository, create a CodeBuild for building the artifact, and the artifact is being stored (not deployed) to a S3 bucket. Difference is that I can't get it to deploy to the root folder of the bucket instead of a subfolder, seems like the process is not made for that. I need it to be on a root folder because how the web access is configured.

For the deployment process, I was taking a look at CodeDeploy but it doesn't actually let me deploy to S3 bucket, it only uses the bucket as an intermediary for deployment to a EC2 instance. So far I get the feeling CodeDeploy is useful only for deployments involving EC2.

This tutorial with a similar requirement than mine, uses CodePipeline and CodeBuild, but the deployment step is actually a aws s3 sync command (same as I was doing on Gitlab), and the actual deployment step on CodePipeline is disabled.

I was looking into a solution which involves using AWS features made for this specific purpose, but I can't find any.

I'm also aware of LambCI, but to me looks like what CodePipeline / CodeBuild is doing, storing artifacts (not deploying to the root folder of the bucket). Plus, I'm looking for an option which doesn't require me to learn or deploy new configuration files (outside AWS config files).

Is this possible with the current state of AWS features?

3

3 Answers

8
votes

Today AWS has announced as a new feature the ability to target S3 in the deployment stage of CodePipeline. The announcement is here, and the documentation contains a tutorial available here.

Using your CodeBuild/CodePipeline approach, you should now be able to choose S3 as the deployment provider in the deployment stage rather than performing the sync in your build script. To configure the phase, you provide an S3 bucket name, specify whether to extract the contents of the artifact zip, and if so provide an optional path for the extraction. This should allow you to deploy your content directly to the root of a bucket by omitting the path.

6
votes

I was dealing with similar issue and as far as I was able to find out, there is no service which is suitable for deploying app to S3.

AWS CodeDeploy is indeed for deploying code running as server.

My solution was to use CodePipeline with three stages:

  1. Source which takes source code from AWS CodeCommit
  2. Build with AWS CodeBuild
  3. Custom lambda function which after successful build takes artifact from S3 artifact storage, unzip it and copies files to my S3 website host.

enter image description here

I used this AWS lambda function from SeamusJ https://github.com/SeamusJ/deploy-build-to-s3

Several changes had to be made, I used node-unzip-2 instead of unzip-stream for unziping artifict from s3.

Also I had to change ACLs in website.ts file

2
votes

Uploading from CodeBuild is currently the best solution available.

There's some suggestions on how to orchestrate this deployment via CodePipeline in this answer.