Deploy dockerized app in aws ecs with fresh new vpc.
Tags: aws, devops, ecs, fargate
Published: 2020-05-25 15:20:30


After struggling for few days to deploy a dockerrized applicatin in ECS, found the solution & simplified into script, so that anyone can deploy easily simply replacing app folder.

This example is for deployment of App with fresh new VPC. Vpc will be created during ecs/fargate deployment.

####git location
https://github.com/rajeshkumarbehura/vpc_ecs_app_deploy

### Requirement an application to deploy in Aws ECS
1. Project must dockerized. In my repo, its demo-app folder which is nestjs based simple application.You can have java/nodejs/python any kind of project as long as it is dockerized.
2. aws cli & terraform cli must be installed in your command line. 
      ```aws --version 
       terraform --version``` 
3. aws details must be avaialble
     export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXX
     export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXX
     export AWS_REGION=ap-southeast-1
     export AWS_PROFILE=dev   // dev profile must match in terraform/providers.ts file

4. It will create fresh new VPC for ecs/fargate deployment.

5. After successfully deployment, terraform will print the alb path as
```demoapp_alb_hostname = dev-demoapp-XXXXXXX.ap-southeast-1.elb.amazonaws.com```

####Steps to deploy
Clone the project into your local.

#####a. Validate aws profile
```export AWS_PROFILE=dev```
this dev profile name must match with deploy/terraform/provider.tf 
```
# Specify the provider and access details
provider "aws" {
  shared_credentials_file = "$HOME/.aws/credentials"
  profile                 = "dev"
  region                  = var.aws_region
}
``` 

#####b. Update shell script file
Two shell script files exist inside deploy folder.
1. create_repo_in_ecr.sh :
Update aws profile details in this file. This file will create aws ecr repository for project. And print a link after successulry created.
Eg - ```XXXXXX.dkr.ecr.ap-southeast-1.amazonaws.com/demo_app_repo```
  This is ECR_REPO_PATH for your project.
2. push_image_to_ecr_&_deploy_ecs.sh :
Update aws profile details & ECR_REPO_PATH. This file will follows below step.
 - create docker container, 
 - container will tagged for ecr repo, 
 - push into ecr repository  
 - application deploy into aws ecs from ecr.

#####c. Execution
Now in final steps, after updating above details, go to deploy folder in command line.
1. Run create_repo_in_ecr.sh file for creating repo.
2. Run push_image_to_ecr_&_deploy_ecs.sh to deploy application

#####d. Destroy in Aws
Cleaning or deleteing all resources from aws, use below steps-
1. Go to deploy/terraform folder location in command line & run below command
``` 
terraform destroy 
```

####How to deploy your own application
1. Replace your application inside demo-app content & make sure your application working dockerized. Please do not change demo-app 
2. Create health check api - api/index. This is configured inside terraform/variable.tf file in terraform folder
```variable "demoapp_health_check_path" {
  default = "/api/index"
}```
3. Optional step- to create your all configuration with a new name, do a find all for "demoapp" text inside terraform folder and replace with ur own text. For example- replace "demoapp" text with "customerapp". Then do terraform plan, and check all things are working or not. if any place, please rename according to your text.

Play with terraform, ecr & ecs to explore more.