Elastic Beanstalk
Elastic Beanstalk is AWS's platform as a service. It allows us to launch our application without having to maintain the underline infrastructure. While we are responsible for our EC2 instances here, AWS will provide the updates that we can apply either automatically or manually.
Elastic Beanstalk comes with out of the box autoscaling and ELB that AWS manages. When we deploy Elastic Beanstalk environment, AWS will provide us with a CNAME that can be used in Route53 to point our domain name.
Let's first create a file with the name iam.tf,
# iam roles
resource "aws_iam_role" "app-ec2-role" {
name = "app-ec2-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_instance_profile" "app-ec2-role" {
name = "app-ec2-role"
role = aws_iam_role.app-ec2-role.name
}
# service
resource "aws_iam_role" "elasticbeanstalk-service-role" {
name = "elasticbeanstalk-service-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "elasticbeanstalk.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# policies
resource "aws_iam_policy_attachment" "app-attach1" {
name = "app-attach1"
roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWebTier"
}
resource "aws_iam_policy_attachment" "app-attach2" {
name = "app-attach2"
roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkMulticontainerDocker"
}
resource "aws_iam_policy_attachment" "app-attach3" {
name = "app-attach3"
roles = [aws_iam_role.app-ec2-role.name]
policy_arn = "arn:aws:iam::aws:policy/AWSElasticBeanstalkWorkerTier"
}
resource "aws_iam_policy_attachment" "app-attach4" {
name = "app-attach4"
roles = [aws_iam_role.elasticbeanstalk-service-role.name]
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth"
}Next a file with the name elasticbeanstalk.tf,
Generate ssh keys,
Initialize the providers,
Apply the changes,
Don't forget to clean up once experiments are done,
Last updated
Was this helpful?