This is a small laboratory of the course "AYGO" at Escuela Colombiana de IngenierĂa Julio Garavito and part of the Informatics's master degree.
Create a simple reactive application and build a script to deploy it automatically to AWS EC2 instances
- deploycode : This folder contains the scripts required to deploy the web app to the AWS infrastructure
- deployinfrastructure: This folder contains the scripts to provision the infrastructure in our AWS account
- lab2app : webapp built in expresjs
- pipeline.sh: entrypoint of this project, this will simulate a pipeline to provision the infrastructure and deploy our app to that infrastructure.
- destroy.sh : script to destroy all the AWS resources and the keys created locally.
The solution is composed of a variety of tools: AWS-cdk
, AWS-sdk
and expressjs
.
For running this project it's required to have installed and configured in your env
- aws-cli
- aws-cdk
- node >10 and npm > 6
- Aws account and aws credential configured (aws configure)
The app is a simple nodejs
application built with the expressjs
framework, this app can be extended but most of the effort of this laboratory was spent in the other components.
From this point the pipeline is just a bash
script that will help us to provision the infrastructure and deploy our application to that infrasctructure.
The pipeline is split in 3 stages
- Create an AWS Key pair
- Provision the infrastructure
- Deploy code
This is a nodejs
script that is using aws-sdk
to create a Key pair, this key is required later when creating the EC2 instances
A private key will be also stored locally with the name key.pem
, this will needed to connect via ssh to the EC2 instances.
This step is using aws-cdk
to create the EC2 instances and all the other components around them (VPN, PublicIP, Security groups, etc). AWS-CDk is a tool to define Infrastructure as Code, so first we need to compile the code running cdk synth
, this will generate a Cloudformation template file locally.
This is an example of how to define a security group using cdk constructs.
Then we can create the resources in our AWS account with the command cdk deploy
.
Once the deployment has finished we can see a CloudFormation
stack created with all the resources, in this case 29 resources where created including IAM roles, EC2 instances a VPN and a security group.
This is a nodejs
script that is using aws-sdk
to obtain the access information of the instances created in the previous step, then using a ssh client (node-ssh) the script will download, build and run the application (docker, npm, git)
First the script finds all the instances created in the previous step using the tag "stackName"
Then using a node-ssh client and the private key create in the step 1 a bunch of commands are executed to install docker and git, then this repository is downloaded, and finally the docker container is created running in with port 80.
Running docker service:
Running the container with port 80
This step is repeated for each EC2 instance found.
The web app is accessible from the EC2 public domain.
Testing the app after the pipeline
finished
This application can be extended to any web application-
- Pipeline deployed
- Try Jenkins or another CI/CD tool
- Explore gitlab devops