Continuously Deploy Your App From GitHub to Amazon ECS
Join the DZone community and get the full member experience.Join For Free
Amazon provides a hosted Container Service for Docker called EC2 Container Service (ECS) as part of its container focused suite of services. Many of our customers seem to love the ECR (EC2 Container Registry) and ECS combination to store and run their applications. Amazon ECS can be accessed by going to your AWS Management Console, selecting EC2 Container Service from the list of Services.
In part I of this series, we demonstrated a simple scenario where we built and pushed a Docker image to ECR as part of the CI build workflow. In this blog post, I will show how you can set up deployment of the same sample application into Amazon ECS, In the last part of this series, I'll show how you can complete your Continuous Delivery pipeline with deployment into subsequent environments, promotion workflows between environments, and release management.
If you want to see an extended version of this scenario in action, attend our webinar in February.
Before you start, please follow all steps shown in this blog. You should have enabled the sample application for CI and run a build which will build and push a Docker image to Amazon ECR. The following steps assume you already have all this in place. Now, for the next steps.
Switch to the pipelines branch:
If you followed all steps in the Pushing your Docker image to Amazon ECR blog, you will have a forked version of the sample application. From your command line, switch to the pipelines branch by running git checkout pipelines
Create a cluster on Amazon ECS:
1. Before you can deploy your application, you will need a cluster on ECS with at least one machine. To create a cluster, follow the instructions in the Amazon ECS docs.
Note down your cluster name and region.
Edit pipeline configuration ymls:
2. The pipelines branch contains 2 files: shippable.resources.yml and shippable.jobs.yml. The resources file contains config for all resources needed for your pipeline, like clusters, key-value paramaters, pointer to docker image(s), etc. A full list of resources is available in our docs. The jobs file contains config for the jobs in your pipeline, like manifest generation, deployments, releases, etc. A complete list of jobs is available in our docs.
Update shippable.resources.yml and make the following edits:
update your Amazon ECR integration in the integration: tag
update the path to your Docker image in the sourceName: tag
update the tag of the image you've pushed to Amazon ECR in the versionName: tag
update your Amazon ECS integration in the integration: tag
replace demo-shippable-ecs-test with your cluster name
replace us-east-1 with the region where your cluster is located
Commit and push your changes.
Understand the configuration:
Before you proceed with setting up this pipeline on Shippable, let's take a moment to understand what it does.
The resources configured in shippable.resources.yml are:
shipdemo-img is an image resource for the image to be deployed.
shipdemo-img-options is a dockerOptions resource which specifies options for the container, like memory, port mappings, etc.
ecs-test-cluster is a cluster resource specifying where the demo application should be deployed
The jobs configured in shippable.jobs.yml are:
shipdemo-manifest is a manifest job that creates a new service manifest each time the image dv-img is updated.
shipdemo-test-deploy is a deploy job that deploys the manifest dv-man to the Test cluster env-test-ecs
Seed your pipeline in Shippable:
3. Seed your pipeline by following the instructions below:
From the Shippable dashboard, go to the Subscription where you forked the sample repo
Follow instructions on the Pipelines page to seed your pipeline. Please make sure to select the pipelines branch while seeding the pipeline.
Go to the SPOG pill menu of your Pipelines tab and voila! You should see your pipeline there.
Deploy to Test:
4. Right click on the shipdemo-manifest job in the SPOG view and click on Run. This will run the manifest job which creates a new service manifest. The deploy job is set up to run after manifest finishes, so it will be automatically triggered.
Check out your deployed demo on ECS:
5. You can now go to your AWS management console and navigate to the deployed application by following the steps below:
Go to your ECS cluster by navigating to EC2 Container Service and clicking on the cluster name
Click on the Service Name starting with shipdemo. This will take you to the Tasks page
Click on the Task to navigate to the containers page.
Expand the container to view the IP address of your deployed application
Click on the IP address will open up a new browser tab and show you the running application.
Connect your CI to the newly created pipeline:
Now that you have your pipeline, you should connect it to your CI. On completing this step, every code change to your sample application will trigger a deployment to the node cluster we set up in previous steps.
6. Create an API token for your account. To do this, go to your Account Settings by clicking on the gear icon in the top navbar. Then click on API tokens in the left sidebar menu and create a token. Copy the token since you won't be able to see it again.
7. Next, create an Event Trigger integration. Go to Integrations in the left sidebar menu of Account Settings and then click on Add Integration. Select Event Trigger from the list and complete the settings as shown below. Please make sure you update the Authorization textbox in the format apiToken <token-value>. The resource name should be set to the image resource shipdemo-img. Save the integration.
8. Update shippable.yml in the master branch of your forked repository which contains the sample application. Uncomment the notifications section and include the event trigger name you created in the earlier step.
- integrationName: #your event trigger integration name
Commit and push to the master branch of your forked repository. This should trigger a build which will create a new version of the manifest and redeploy to the Test environment on ECS. Watch the SPOG window to see all this in real time!
Change your app and see the changes automatically deployed to ECS:
Now make a simple change to the app in the master branch by changing the color from #fff to som other color in the css stylesheet static/app.css
Commit and push your changes. Your app will be continuously deployed to your Test environment and when you see deployment finish in SPOG, you can verify that your deployed app has been updated with the new color.
In part III of this series, I will show you how to complete the rest of the CD pipeline, including setting up additional environments, promotion workflows, release management, etc. Stay tuned!
Published at DZone with permission of Pavan Belagatti, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.