DEV Community

Nishanth-Gowda
Nishanth-Gowda

Posted on

Getting Started with AWS Elastic Beanstalk and Terraform

Amazon Web Services (AWS) Elastic Beanstalk is a service that makes it easy to deploy, run, and scale web applications. In this guide, we will go through the steps to create your first Elastic Beanstalk application using Terraform.

Prerequisites Before we begin, you will need the following:

  • An AWS account
  • Terraform installed on your local machine
  • Basic knowledge of Terraform and AWS

Step 1: Create a Directory and Files. Create a new directory and navigate to it in your terminal. Create three files within this directory: main.tf, vars.tf, and provider.tf.

In main.tf, paste the following configuration, which will create an Elastic Beanstalk application, environment, and associated resources.

nes (85 sloc) 2.32 KB
terraform {
  #############################################################
  ## AFTER RUNNING TERRAFORM APPLY (WITH LOCAL BACKEND)
  ## YOU WILL UNCOMMENT THIS CODE THEN RERUN TERRAFORM INIT
  ## TO SWITCH FROM LOCAL BACKEND TO REMOTE AWS BACKEND
  #############################################################
   backend "s3" {
    bucket         = "ec2-provisioned-terraform-state" # REPLACE WITH YOUR BUCKET NAME
    key            = "input-output-vars/import-bootstrap/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-atomic-lock-for-backend"
    encrypt        = true
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }
}

resource "aws_elastic_beanstalk_application" "myelasticapp" {
  name = var.myelasticapp
  description = "My Test App"
}

resource "aws_elastic_beanstalk_environment" "beanstalkappenv" {
  name = var.beanstalkappenv
  application = aws_elastic_beanstalk_application.myelasticapp.name
  solution_stack_name = var.solution_stack_name
  tier = var.tier

  setting {
    namespace = "aws:ec2:vpc"
    name = "VPCID"
    value = var.vpc_id
  }

  setting {
    namespace = "aws:autoscaling:launchconfiguration"
    name = "IamInstanceProfile"
    value = "aws-elasticbeanstalk-ec2-role"
  }

  setting {
    namespace = "aws:ec2:vpc"
    name = "AssociatePublicIpAddress"
    value = "True"
  }

  setting {
    namespace  = "aws:ec2:vpc"
    name       = "Subnets"
    value      = join(",", var.public_subnets)
  }

  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.medium"
  }
  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBScheme"
    value     = "internet facing"
  }
  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"
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a file named vars.tf. This file basically contains the variables that will be used while provisioning the resource. These variables will reference to the main.tf file. While defining the vpc please make sure to change to your vpc and while defining subnets use 2 subnets different availability zone.

variable "myelasticapp" {
  default = "MyApplication"
}

variable "beanstalkappenv" {
  default = "myenv"
}

variable "solution_stack_name" {
  default = "64bit Amazon Linux 2 v3.4.4 running Python 3.8"
}

variable "tier" {
  default = "WebServer"
}

variable "vpc_id" {
    default = "vpc-0ddb47357d6007df9"
}
variable "public_subnets" {
    default = ["subnet-0a9815b576a7b0f79", "subnet-03b4f674ef84bac0d"]
}
Enter fullscreen mode Exit fullscreen mode

step 3: Create a file named provider.tf. This contains the information regarding the cloud provider and the region.

provider "aws" {
    region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

step 4: Use terraform validate command to validate the configuration file.

step 5: Use terraform apply command to start the provisioning of resources.

step 6: Use terraform destroy command to terminate all the provisioned resources.

Happy Learning :)

Top comments (0)