Create an Elastic Beanstalk Application using Terraform.

Create an Elastic Beanstalk Application using Terraform.

Introduction.

Elastic Beanstalk is a fully managed service from AWS that allows developers to easily deploy and manage applications. With Elastic Beanstalk, users can focus on writing code without worrying about the underlying infrastructure. It supports various programming languages and frameworks, including Java, Python, .NET, Node.js, and more. Terraform is an Infrastructure as Code (IaC) tool that allows users to define and provision infrastructure using declarative configuration files. Using Terraform to create an Elastic Beanstalk application involves defining resources like the application itself and the environment in which it runs. This approach enables version-controlled, repeatable deployments and simplifies infrastructure management. By leveraging Terraform’s automation and Elastic Beanstalk’s managed environment, developers can streamline deployment processes, scale applications, and ensure high availability. The integration of both tools makes it easy to set up and maintain complex application environments in the cloud.

STEP 1: Go to vscode and Open your Folder and create a variable.tf file.

  • Enter the following Script.
variable "access_key" {
    description = "Access key to AWS console"
}

variable "secret_key" {
    description = "Secret key to AWS console"
}
variable "region" {
    description = "AWS region"
}
Screenshot 2025 02 28 063618

STEP 2: Click on create a terraform.tfvars file.

  • Enter the following command.
region = "us-east-1"

access_key = "<YOUR_ACCESS_KEY>"

secret_key = "<YOUR_SECRET_KEY>"
Screenshot 2025 02 28 063959

STEP 3: Next, Create a main.tf file.

provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}
resource "aws_elastic_beanstalk_application" "myapp"{
    name        = "test"
    description = "Sample Test Application"
}
resource "aws_iam_instance_profile" "subject_profile" {
  name = "test_role_new"
  role = aws_iam_role.role.name
}
resource "aws_iam_role" "role" {
  name = "test_role_new"
  path = "/"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Principal": {
               "Service": "ec2.amazonaws.com"
            },
            "Effect": "Allow",
            "Sid": ""
        }
    ]
}
EOF
}
resource "aws_iam_role_policy_attachment" "role-policy-attachment" {
  for_each = toset([
    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier", 
    "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker",
    "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier",
  ])

  role = "${aws_iam_role.role.name}"
  policy_arn = each.value
}

STEP 4: To create an elastic environment for the application, paste the below code in the main.tf

resource "aws_elastic_beanstalk_environment" "env" {
  name                = "environment"
  application         = aws_elastic_beanstalk_application.whizapp.name
  solution_stack_name = "64bit Amazon Linux 2 v3.4.1 running Corretto 17"

setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name = "IamInstanceProfile"
    value = "${aws_iam_instance_profile.subject_profile.name}"
  }

 setting {
    namespace = "aws:elasticbeanstalk:environment:process:default"
    name      = "MatcherHTTPCode"
    value     = "200"
  }
 setting {
    namespace = "aws:elasticbeanstalk:environment"
    name      = "LoadBalancerType"
    value     = "application"
  }

   setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name      = "InstanceType"
    value     = "t2.micro"
  }
   setting {
    namespace = "aws:autoscaling:asg"
    name      = "MinSize"
    value     = 1
  }
  setting {
    namespace = "aws:autoscaling:asg"
    name      = "MaxSize"
    value     = 2
  }
  setting {
    namespace = "aws:elasticbeanstalk:healthreporting:system"
    name      = "SystemType"
    value     = "enhanced"
  }
}
Screenshot 2025 02 28 064448
Screenshot 2025 02 28 064802
Screenshot 2025 02 28 064848

STEP 5: Create output.tf file.

output "aws_beanstalk_app" {
  value = aws_elastic_beanstalk_application.myapp.arn
}
Screenshot 2025 02 28 065001

STEP 6: Go to terminal and enter the terraform init command.

Screenshot 2025 02 28 065241

STEP 7: Next, Enter the terraform plan and terraform apply command.

Screenshot 2025 02 28 065627
Screenshot 2025 02 28 065758

Conclusion.

In conclusion, using Terraform to create and manage an Elastic Beanstalk application offers numerous benefits, including infrastructure automation, scalability, and ease of management. By defining your Elastic Beanstalk application and environment in code, you can ensure consistent deployments, streamline updates, and minimize the risk of manual errors. Terraform’s declarative approach to infrastructure management allows for version control and collaboration, which enhances the flexibility and reliability of your application deployment pipeline. Leveraging both AWS Elastic Beanstalk’s managed environment and Terraform’s powerful provisioning capabilities creates a seamless workflow for developers, enabling faster, more efficient development cycles while maintaining full control over the infrastructure.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *