How to Launch AWS Elastic beanstalk using Terraform

No comments

Working on Amazon web service itself is a amazing thing as there are lots of services which are provided by Amazon to make you free from hassle of setting up entire infrastructure step by step . Suppose you want to create a 3 instance and align a load balancer in front of them and hosting a website and store all data in database.

No doubt we have amazon provided AWS EC2 service, for database we have AWS RDS service in case of relational database and for load balancer amazon provides ELB i.e.. elastic load balancers. But what if we have can have a common platform to work with all these services together and work ? Wouldn’t that be much easier for all of us. Yes that’s correct it will make things much easier, so does the Amazon Elastic beanstalk and is one of the best service one can use Amazon Web Service.

In this tutorial, we will learn how to step up Amazon Elastic beanstalk using terraform on AWS step by step and then upload the code to run one of the simple application.

Table of content

  1. What is Elastic beanstalk?
  2. Prerequisites
  3. How to Install Terraform on Ubuntu 18.04 LTS
  4. Terraform Configuration Files and Structure
  5. Launch Elastic beanstalk on Amazon Web Service using Terraform
  6. Conclusion

What is Elastic beanstalk?

AWS Elastic beanstalk is one of the best service one can use on amazon web service tool. It is service which can work with variety of languages such as python , go , ruby, java , .net , PHP for hosting the application. The only thing you need to do in elastic beanstalk is just create your code with any of these high level languages and upload your code in AWS elastic beanstalk, then rest of the things will be taken care by elastic beanstalk itself such as scaling, load balancing , monitoring and so on .

Elastic beanstalk makes the life of developer as well as for cloud admins or sysadmins so easy compared to setting each service individually and interlinking each other.

  • Some of the key benefits of AWS Elastic beanstalk are
    1. It scales the applications up or down as per the required traffic.
    2. As infrastructure is managed and taken care by AWS Elastic beanstalk developers working with admins don’t need to spend much time.
    3. It is fast and easy to setup
    4. You can interlink with lots of other AWS services of your own choice or you can skip it such as linking of application or classic or network load balancer.
Listen to What is AWS Elastic beanstalk?

Prerequisites

  • Ubuntu machine to run terraform preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • Ubuntu machine should have IAM role attached with AWS Elastic beanstalk creation permissions or it is always great to have administrator permissions to work with demo’s.

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Terraform on Ubuntu 18.04 LTS

  • Update your already existing system packages.
sudo apt update
  • Download the latest version of terraform in opt directory
wget https://releases.hashicorp.com/terraform/0.14.8/terraform_0.14.8_linux_amd64.zip
This image has an empty alt attribute; its file name is image-163.png
  • Install zip package which will be required to unzip
sudo apt-get install zip -y
  • unzip the Terraform download zip file
unzip terraform*.zip
  • Move the executable to executable directory
sudo mv terraform /usr/local/bin
  • Verify the terraform by checking terraform command and version of terraform
terraform               # To check if terraform is installed 

terraform -version      # To check the terraform version  
This image has an empty alt attribute; its file name is image-164.png
This image has an empty alt attribute; its file name is image-165.png
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.

Terraform Configuration Files and Structure

Let us first understand terraform configuration files before running Terraform commands.

  • main.tf : This file contains code that create or import other AWS resources.
  • vars.tf : This file defines variable types and optionally set the values.
  • output.tf: This file helps in generating of the output of AWS resources .The output is generated after the terraform apply command is executed.
  • terraform.tfvars: This file contains the actual values of variables which we created in vars.tf
  • provider.tf: This file is very important . You need to provide the details of providers such as AWS , Oracle or Google etc. so that terraform can make the communication with the same provider and then work with resources.

Launch AWS Elastic beanstalk on AWS using Terraform

  • Now you will create all the configuration files which are required for creation of Elastic beanstalk on AWS account .
  • Create a folder in opt directory and name it as terraform-elasticbeanstalk-demo and create all the files under this folder.

main.tf

# Create elastic beanstalk application


resource "aws_elastic_beanstalk_application" "elasticapp" {
  name = var.elasticapp
}

# Create elastic beanstalk Environment

resource "aws_elastic_beanstalk_environment" "beanstalkappenv" {
  name                = var.beanstalkappenv
  application         = aws_elastic_beanstalk_application.elasticapp.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"
  }

}

vars.tf

variable "elasticapp" {
  default = "myapp"
}
variable "beanstalkappenv" {
  default = "myenv"
}
variable "solution_stack_name" {
  type = string
}
variable "tier" {
  type = string
}

variable "vpc_id" {}
variable "public_subnets" {}
variable "elb_public_subnets" {}

provider.tf

provider "aws" {
  region = "us-east-2"
}

terraform.tfvars

vpc_id              = "vpc-XXXXXXXXX"
Instance_type       = "t2.medium"
minsize             = 1
maxsize             = 2
public_subnets     = ["subnet-XXXXXXXXXX", "subnet-XXXXXXXXX"] # Service Subnet
elb_public_subnets = ["subnet-XXXXXXXXXX", "subnet-XXXXXXXXX"] # ELB Subnet
tier = "WebServer"
solution_stack_name= "64bit Amazon Linux 2 v3.2.0 running Python 3.8"
  • Now your files and code are ready for execution . Initialize the terraform
terraform init
  • Terraform initialized successfully , now its time to see the plan which is kind of blueprint before deployment. We generally use plan to confirm if correct resources is going to provisioned or deleted.
terraform plan
  • After verification , now its time to actually deploy the code using apply.
terraform apply

Great Job, terraform commands execution was done successfully. Now we should have AWS Elastic beanstalk launched in AWS.

  • Now go to AWS account and search for AWS Elastic beanstalk service and after it gets open click on myenv and check the URL

Conclusion

In this tutorial, we demonstrated some benefits of Amazon Elastic beanstalk and learnt how to set up Amazon Elastic beanstalk using terraform on AWS step by step .

Hope this tutorial will help you in understanding the terraform and provisioning the elastic beanstalk on Amazon cloud. Please share with your friends

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s