Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Your containerized application with IAC on AWS โ€” Pt.3


๐Ÿ“š Your containerized application with IAC on AWS โ€” Pt.3


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Hi Folks! This will be the final post in our series on infrastructure and containers. We will utilize Terragrunt and our infrastructure in this section, and at the conclusion, we will have our application operating on Fargate on AWS.

The docker image Iโ€™ll be using in this lesson comes from Sonic, an old game that many people associate with their early years. You may use this image or find it on my dockerhub, whichever you would like.

DIRECTORIES

Again, Iโ€™ll leave our directory structure here so you can guide yourself:

app
modules
    โ”œโ”€โ”€ amazon_vpc
    โ”œโ”€โ”€ aws_loadbalancer
    โ”œโ”€โ”€ aws_fargate
    โ”œโ”€โ”€ aws_roles
    โ”œโ”€โ”€ aws_ecs_cluster
    โ””โ”€โ”€ aws_targetgroup
    โ””โ”€โ”€ aws_certificate_manager

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
            โ”œโ”€โ”€ aws_ecs
            โ”‚   โ”œโ”€โ”€ cluster
            โ”‚   โ””โ”€โ”€ service
            โ”œโ”€โ”€ aws_loadbalancer
            โ”œโ”€โ”€ amazon_vpc
            โ”œโ”€โ”€ aws_targetgroup
            โ”œโ”€โ”€ aws_roles
            โ”œโ”€โ”€ aws_certificate_manager
            โ””โ”€โ”€ terragrunt.hcl

TERRAGRUNT

First, letโ€™s look at our terragrunt.hcl, located in us-east-1. It will be used for all common variables in our code, as well as for creating our backend settings and the lock in the dynamodb database.

Typical variables are going to be region, project_name, domain_name, env, host_headers and container_port.

terragrunt.hcl

remote_state {
  backend = "s3"
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket           = "sonic-iac-series"
    key              = "dev/${path_relative_to_include()}/terraform.tfstate"
    region           = "us-east-1"
    encrypt          = true
    dynamodb_table   = "terraform-state-lock"
  }
}

inputs = {
   region            = "us-east-1"
   project_name      = "sonic-iac"
   env               = "dev"
   domain_name       = "your domain"
   host_headers      = "sonic.your domain"
   container_port    = "8080"

  tags = {
     ambiente        = "dev"
     projeto         = "sonic-iac"
     plataforma      = "aws"
     gerenciado      = "terraform/terragrunt"
   }
}

generate "provider" {
    path      = "provider.tf"
    if_exists = "overwrite"
    contents = <<EOF
provider "aws" {
  profile   = "default"
  region    = "us-east-1"
}
EOF
}

VPC

The first resource to be created will be the VPC, as it will be needed for most of our resources.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ amazon_vpc
                 โ””โ”€โ”€ terragrunt.hcl

We will use a range of /25, starting with IP 172.35.0.221, to construct our VPC. Four subnets โ€” two public and two private โ€” will be created inside it.

  • VPC: 172.35.0.128/25
  • Public Subnet 1: 172.35.0.128/27
  • Public Subnet 2: 172.35.0.160/27
  • Private Subnet 1: 172.35.0.192/27
  • Private Subnet 2: 172.35.0.224/27 These code files will be created within:

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

inputs = {
    vpc_cidr_block              = "172.35.0.128/25"
    public_subnet1_cidr_block   = "172.35.0.128/27"
    public_subnet2_cidr_block   = "172.35.0.160/27"
    private_subnet1_cidr_block  = "172.35.0.192/27"
    private_subnet2_cidr_block  = "172.35.0.224/27"
    availability_zone1 = "us-east-1a"
    availability_zone2 = "us-east-1b"
}
terraform {
  source = "../../../../modules/amazon_vpc"
  extra_arguments "custom_vars" {
    commands = [
        "apply",
        "plan",
        "import",
        "push",
        "refresh"
    ]
  }
}

IAM PERMISSIONS

The next thing to be created will be permissions for our resources.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_roles
                 โ””โ”€โ”€ terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "../../../../modules/aws_roles"
  extra_arguments "custom_vars" {
    commands = [
        "apply",
        "plan",
        "import",
        "push",
        "refresh"
    ]
  }
}

AWS CERTIFICATE MANAGER

These are the configurations for applying our certificate; we will generate the certificate and use our domain to validate it.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_certificate_manager
                 โ””โ”€โ”€ terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "../../../../modules/aws_certificate_manager"
  extra_arguments "custom_vars" {
    commands = [
        "apply",
        "plan",
        "import",
        "push",
        "refresh"
    ]
  }
}

AWS LOAD BALANCER

Letโ€™s set up our loadbalancer using Terragrunt now. This will help distribute our traffic and guarantee that our application is highly available.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_loadbalancer
                 โ””โ”€โ”€ terragrunt.hcl

Our Terragrunt setup looks like this. Itโ€™s important to note that in order to increase everythingโ€™s dynamic nature, we use dependencies between modules.

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

dependency "vpc" {
  config_path = "../amazon_vpc"
}

dependency "acm" {
  config_path = "../aws_certificate_manager"
}

inputs = {
  vpc_id       = dependency.vpc.outputs.vpc_id
  subnet_id_1  = dependency.vpc.outputs.public_subnet1_id
  subnet_id_2  = dependency.vpc.outputs.public_subnet2_id
  alb_internal = false
  certificate_arn = dependency.acm.outputs.acm_arn
  priority_listener_rule  = "1"
}
terraform {
  source = "../../../../modules/aws_loadbalancer"
  extra_arguments "custom_vars" {
    commands = [
      "apply",
      "plan",
      "import",
      "push",
      "refresh"
    ]
  }
}

AWS TARGET GROUP

Here we will configure our Target Group with Terragrunt, it is super essential for directing traffic to the correct servers for our application.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_targetgroup
                 โ””โ”€โ”€ terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}
dependency "loadbalancer" {
  config_path = "../aws_loadbalancer"
}  
  dependency "vpc" {
  config_path = "../amazon_vpc"
}

dependency "acm" {
  config_path = "../aws_certificate_manager"
}

inputs = {
  vpc_id                  = dependency.vpc.outputs.vpc_id
  subnet_id_1             = dependency.vpc.outputs.public_subnet1_id
  subnet_id_2             = dependency.vpc.outputs.public_subnet2_id
  certificate_arn         = dependency.acm.outputs.acm_arn
  listener_ssl_arn        = dependency.loadbalancer.outputs.listener_ssl_arn
  priority_listener_rule  = "2"
  health_check_path       = "/"
}

terraform {
  source = "../../../../modules/aws_targetgroup"
  extra_arguments "custom_vars" {
    commands = [
      "apply",
      "plan",
      "import",
      "push",
      "refresh"
    ]
  }
}

ECS CLUSTER

In this step we will create our ECS cluster that will host our application.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_ecs
                 โ””โ”€โ”€ cluster
                       โ””โ”€โ”€ terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}

terraform {
  source = "../../../../../modules/aws_ecs_cluster"
  extra_arguments "custom_vars" {
    commands = [
        "apply",
        "plan",
        "import",
        "push",
        "refresh"
    ]
  }
}

FARGATE AND ECR

We will construct our fargate service, the repository in the ECR, and a record on our domain as the final configuration file.

terragrunt
    โ””โ”€โ”€ dev
        โ””โ”€โ”€ us-east-1
             โ””โ”€โ”€ aws_ecs
                 โ””โ”€โ”€ service
                       โ””โ”€โ”€ terragrunt.hcl

terragrunt.hcl

include {
  path = find_in_parent_folders()
}
dependency "loadbalancer" {
  config_path = "../../aws_loadbalancer"
}  
  dependency "vpc" {
  config_path = "../../amazon_vpc"
}
dependency "role" {
  config_path = "../../aws_roles"
}

dependency "targetgroup" {
  config_path = "../../aws_targetgroup"
}

dependency "cluster" {
  config_path = "../cluster"
}

inputs = {
  vpc_id                = dependency.vpc.outputs.vpc_id
  subnet_id_1           = dependency.vpc.outputs.private_subnet1_id
  subnet_id_2           = dependency.vpc.outputs.private_subnet2_id
  alb_dns_name          = dependency.loadbalancer.outputs.alb_dns_name
  sg_alb                = dependency.loadbalancer.outputs.alb_secgrp_id
  target_group_arn      = dependency.targetgroup.outputs.tg_alb_arn
  cluster_arn           = dependency.cluster.outputs.cluster_arn
  ecs_role_arn          = dependency.role.outputs.ecs_role_arn
  instance_count        = "1"
  container_vcpu        = "512"
  container_memory      = "1024"
  aws_account_id        = "your account number"
}

terraform {
  source = "../../../../../modules/aws_fargate"
  extra_arguments "custom_vars" {
    commands = [
        "apply",
        "plan",
        "import",
        "push",
        "refresh"
    ]
  }
}

APPLY

After the entire structure has been created, you must apply terragrunt to all directories that contain terragrunt.hcl in the following order.

terragrunt/dev/us-east-1
terragrunt/dev/us-east-1/amazon_vpc
terragrunt/dev/us-east-1/aws_roles
terragrunt/dev/us-east-1/aws_certificate_manager
terragrunt/dev/us-east-1/aws_loadbalancer
terragrunt/dev/us-east-1/aws_targetgroup
terragrunt/dev/us-east-1/aws_ecs/cluster
terragrunt/dev/us-east-1/aws_ecs/fargate
Use this command on terminal to apply. You need use in each directory

terragrunt apply

or in the root folder use:
terragrunt run-all apply

ECR

Now we have applied all our infrastructure and our ECR repository has been created, we must upload our image for use in our container.

The image must be downloaded from Docker Hub as an initial step. You can use another image if you prefer or your own from your application.

use this command to download my sonic image:
docker pull shescloud/sonic-the-hedgehog

ECR

ECR

ECR

TESTING

I used a domain I had and our application was temporarily hosted at sonic.shescloud.tech.

TESTING

DESTROY
If you are using it for study, or as a way to complete a test, donโ€™t forget to destroy all resources at the end to avoid unnecessary costs. To delete everything, we will do a process similar to apply, but in the opposite way.

Before deleting everything via terragrunt, you need to access your AWS account, go to the ECR service and delete the image from the repository. After completing this step, you can proceed with destroying each of the repositories.

Image description

Now, you must destroy to all directories that contain terragrunt.hcl in the following order.

  1. terragrunt/dev/us-east-1/aws_ecs/fargate
  2. terragrunt/dev/us-east-1/aws_ecs/cluster
  3. terragrunt/dev/us-east-1/aws_targetgroup
  4. terragrunt/dev/us-east-1/aws_loadbalancer
  5. terragrunt/dev/us-east-1/aws_roles
  6. terragrunt/dev/us-east-1/aws_certificate_manager
  7. terragrunt/dev/us-east-1/amazon_vpc
  8. terragrunt/dev/us-east-1

Use this command on terminal to destroy. You need use in each directory

terragrunt destroy

or in the root folder use:
`terragrunt run-all destroy

`

GITHUB

You can check the repository with the code on my github:
https://github.com/shescloud/terraform-terragrunt-fargate

And thatโ€™s it folks! I hope you enjoyed it and get a lot out of this code. See u soon!

...



๐Ÿ“Œ Your containerized application with IAC on AWS โ€” Pt.1


๐Ÿ“ˆ 52.81 Punkte

๐Ÿ“Œ Your containerized application with IAC on AWS โ€” Pt.2


๐Ÿ“ˆ 52.81 Punkte

๐Ÿ“Œ Your containerized application with IAC on AWS โ€” Pt.3


๐Ÿ“ˆ 52.81 Punkte

๐Ÿ“Œ DevSecOps with AWS- IaC at scale - Building your own platform - Part 2 - CI for IaC


๐Ÿ“ˆ 45.36 Punkte

๐Ÿ“Œ Leveraging Infrastructure as Code (IaC) for AWS Lambda: A Comparative Analysis of AWS SAM, Terraform, and Serverless Framework


๐Ÿ“ˆ 31.37 Punkte

๐Ÿ“Œ Generate customized, compliant application IaC scripts for AWS Landing Zone using Amazon Bedrock


๐Ÿ“ˆ 30.2 Punkte

๐Ÿ“Œ Sailing Smoothly with AWS Container Registry: Your Gateway to Containerized Applications


๐Ÿ“ˆ 29.45 Punkte

๐Ÿ“Œ Get started quickly with AWS Trainium and AWS Inferentia using AWS Neuron DLAMI and AWS Neuron DLC


๐Ÿ“ˆ 27.32 Punkte

๐Ÿ“Œ AWS App Runner: Deploy containerized web apps and APIs at scale


๐Ÿ“ˆ 26.33 Punkte

๐Ÿ“Œ How to Deploy Containerized Apps on AWS Using ECR and Docker


๐Ÿ“ˆ 26.33 Punkte

๐Ÿ“Œ Enabling distributed tracing for containerized apps with AWSย X-Ray


๐Ÿ“ˆ 26.33 Punkte

๐Ÿ“Œ Three levels of complexity Threat Modeling of Containerized Application


๐Ÿ“ˆ 25.16 Punkte

๐Ÿ“Œ Containerized Application Deployment on Amazon EKS with Jenkins Pipeline


๐Ÿ“ˆ 25.16 Punkte

๐Ÿ“Œ Spring Boot 3 application on AWS Lambda - Part 3 Develop application with AWS Serverless Java Container


๐Ÿ“ˆ 24.98 Punkte

๐Ÿ“Œ Spring Boot 3 application on AWS Lambda - Part 6 Develop application with AWS Lambda Web Adapter


๐Ÿ“ˆ 24.98 Punkte

๐Ÿ“Œ Unleashing the Power of AWS: Revolutionizing Cloud Management Through Infrastructure as Code (IaC)


๐Ÿ“ˆ 24.54 Punkte

๐Ÿ“Œ Terraform pipeline (IaC for AWS)


๐Ÿ“ˆ 24.54 Punkte

๐Ÿ“Œ How to Choose the Right IaC Tool โ€“ AWS CDK, CloudFormation, and Terraform Compared


๐Ÿ“ˆ 24.54 Punkte

๐Ÿ“Œ Automating the Cloud: IaC with AWS CloudFormation and Terraform


๐Ÿ“ˆ 24.54 Punkte

๐Ÿ“Œ Generative (A)IaC in the IDE with Application Composer


๐Ÿ“ˆ 23.37 Punkte

๐Ÿ“Œ How to know youโ€™re getting best value out of your Kubernetes and containerized workload investments


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ Safeguard your containerized workloads using AKS backup | Azure Friday


๐Ÿ“ˆ 22.62 Punkte

๐Ÿ“Œ Securing Your Web Application with AWS WAF and AWS Shield


๐Ÿ“ˆ 22.44 Punkte

๐Ÿ“Œ Iac-Scan-Runner - Service That Scans Your Infrastructure As Code For Common Vulnerabilities


๐Ÿ“ˆ 20.82 Punkte

๐Ÿ“Œ Terraform vs. Pulumi: Which Is Better for Your IaC Requirements?


๐Ÿ“ˆ 20.82 Punkte

๐Ÿ“Œ IBM has acquired HashiCorp, the company behind the tool we know as IaC Terraform. What are your thoughts?


๐Ÿ“ˆ 20.82 Punkte

๐Ÿ“Œ DevSecOps with AWS โ€“ ChatOps with AWS and AWS Developer Tools โ€“ Part 1


๐Ÿ“ˆ 20.49 Punkte

๐Ÿ“Œ AWS Resume Challenge using Pulumi, Golang, AWS S3 and AWS CloudFront


๐Ÿ“ˆ 20.49 Punkte

๐Ÿ“Œ Google Plans to Add Support for Containerized Linux Apps to Chromebooks


๐Ÿ“ˆ 19.5 Punkte

๐Ÿ“Œ Are these a containerized snack?


๐Ÿ“ˆ 19.5 Punkte

๐Ÿ“Œ Death to SQL Server! Long live SQL Server! How containerized SQL Server makes development easier


๐Ÿ“ˆ 19.5 Punkte

๐Ÿ“Œ How Containerized SQL Server Makes Development Easier


๐Ÿ“ˆ 19.5 Punkte

๐Ÿ“Œ Micro Focus Hybrid Cloud Management Containerized Suite Remote Code Execution


๐Ÿ“ˆ 19.5 Punkte

๐Ÿ“Œ How to build and deploy a containerized app to Azure Kubernetes Service (AKS) | Azure Friday


๐Ÿ“ˆ 19.5 Punkte











matomo