Terraform Infrastructure Setup and Automatically build and deploy a Java application to Amazon ECS using a DevSecOps CI/CD pipeline
This project showcases an experience in design, manage, and deploy a secure containerized infrastructure using Terraform, AWS, Git, and CI/CD pipelines. The setup includes an ECS cluster for containerized applications, a RDS instance, and a secure location for storing secrets. The infrastructure is designed to be reusable for both development and production environments.
- Created reusable Terraform code for both development and production environments.
- Set up essential components for a secure infrastructure including IAM roles, security groups, ACLs, and load balancers.
- Stored both Terraform code and application code in a Git repository.
- Application code consists of a simple "Hello, World!!!" webpage with the URL (http://[alb.dns.name]/hello).
- Implemented a CI/CD pipeline using AWS CodePipeline, CodeBuild, CodeDeploy.
- Automates deployments triggered by branch merges:
- Changes merged into the
main
branch are deployed to the production environment. - Changes merged into the
develop
branch are deployed to the testing environment.
- Changes merged into the
- All resources are tagged with
name:csgtest
for easy identification and management.
- Configured ECS Cluster with the module
terraform-aws-modules/ecs/aws
. - Task definitions include environment variables specifying the appropriate environment (development or production).
- Configured AWS Application Load Balancer (ALB) with the module
terraform-aws-modules/alb/aws
.
- Provisioned a secure and scalable RDS instance for database needs with the module
terraform-aws-modules/rds/aws
.
- Used AWS Secrets Manager to securely store and manage secrets.
- Provisioned a secure AWS VPC: private, public, and database subnets with the module
terraform-aws-modules/vpc/aws
- Implemented robust security measures including IAM roles/policies, security groups, and ACLs with module
terraform-aws-modules/security-group/aws
.
- Configured state storage as a key in Amazon S3 bucket with the module
terraform-aws-modules/s3-bucket/aws
Created a continuous integration and continuous delivery (CI/CD) pipeline that automatically builds and deploys a Java application to an Amazon Elastic Container Service (Amazon ECS) cluster on the Amazon Web Services (AWS) Cloud. This pattern uses a greeting application developed with a Spring Boot Java framework and that uses Apache Maven.
This solution will be useful to build the code for a Java application, package the application artifacts as a Docker image, security scan the image, and upload the image as a workload container on Amazon ECS and can be also used as a reference to migrate from a tightly coupled monolithic architecture to a microservices architecture. It also emphasizes on how to monitor and manage the entire lifecycle of a Java application, which ensures a higher level of automation and helps avoid errors or bugs and has been implemented with best DevSecOps Pipeline practices.
The diagram shows the following workflow:
-
Developer will update the Java application code in the main/develop branch of the AWS CodeCommit repository.
-
Amazon CodeGuru Reviewer automatically reviews the code if a Pull Request is submitted and does a analysis of java code as per the best practices and gives recommendations to users.
-
Once the code is pushed to the repository, a AWS CloudWatch event is created.
-
This AWS CloudWatch event triggers the AWS CodePipeline.
-
CodePipeline runs the security scan stage (continuous security).
-
CodeBuild first starts the security scan process in which Dockerfile, Terraform files are scanned using Checkov and application source code is scanned using AWS CodeGuru CLI based on incremental code changes.
-
Next, if the security scan stage is successful, the build stage(continuous integration) is triggered.
-
In the Build Stage, CodeBuild builds the artifact, packages the artifact to a Docker image, scans the image for security vulnerabilities by using Aqua Security Trivy, and stores the image in Amazon Elastic Container Registry (Amazon ECR).
-
The vulnerabilities detected from step 6 are uploaded to AWS Security Hub for further analysis by users or developers, which provides overview, recommendations, remediation steps for the vulnerabilties.
-
Emails Notifications of various phases within the AWS CodePipeline are sent to the users via Amazon SNS.
-
After the continuous integration phases are complete, CodePipeline enters the deployment phase (continuous delivery).
-
The Docker image is deployed to Amazon ECS as a container workload (Task) using AWS CodeDeploy.
├── README.md
├── alb.tf
├── backend.tf
├── ecs.tf
├── iam.tf
├── main.tf
├── namespaces.tf
├── outputs.tf
├── rds.tf
├── s3.tf
├── securitygroups.tf
├── securitymanager.tf
├── variables.tf
├── vpc.tf
├── backend.tfvars
├── terraform.tfvars
├── cicd-diagram.png
├── infra-diagram.png
├── buildspec
│ ├── buildspec.yml
│ └── buildspec_secscan.yaml
├── cf_templates
│ ├── build_deployment.yaml
│ └── codecommit_ecr.yaml
├── code
│ └── app
│ ├── Dockerfile
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ │ └── software
│ │ └── amazon
│ │ └── samples
│ │ └── greeting
│ │ ├── Application.java
│ │ └── GreetingController.java
│ └── resources
│ └── Images
│ └── aws_proserve.jpg
└── securityhub
└── asff.tpl
- .tf,.tfvars: Terraform infrastructure files
├── alb.tf
├── backend.tf
├── ecs.tf
├── iam.tf
├── main.tf
├── namespaces.tf
├── outputs.tf
├── rds.tf
├── s3.tf
├── securitygroups.tf
├── securitymanager.tf
├── variables.tf
├── vpc.tf
├── backend.tfvars
├── terraform.tfvars
- buildspec: BuildSpec yaml files, buildspec.yml (For Build Phase), buildspec_secscan.yaml (For CodeSecurityScan Phase)
buildspec
├── buildspec.yml (Build)
└── buildspec_secscan.yaml(CodeSecurityScan)
- cf_templates: Cloudformation templates
cf_templates
├── build_deployment.yaml (Pipeline Stack Setup)
└── codecommit_ecr.yaml (Codecommit and ECR Setup)
- code: Sample Spring Boot application source code (src folder), Dockerfile and pom.xml
code
└── app
├── Dockerfile
├── pom.xml
└── src
└── main
├── java
│ └── software
│ └── amazon
│ └── samples
│ └── greeting
│ ├── Application.java
│ └── GreetingController.java
└── resources
└── Images
└── aws_proserve.jpg
- securityhub: ASFF template (AWS Security Finding Format, part of AWS SeurityHub service). This format will be used for uploading docker image vulnerabilties details to AWS SecurityHub
securityhub
└── asff.tpl