terraform - error on creating AWS Elastic Beanstalk

Viewed 1104

I am trying to provision an AWS Elastic Beanstalk using terrafrom. Below is the .tf file I have written:

resource "aws_s3_bucket" "default" {
  bucket = "textX"
}

resource "aws_s3_bucket_object" "default" {
  bucket = "${aws_s3_bucket.default.id}"
  key    = "test-app-version-tf--dev"
  source = "somezipFile.zip"
}

resource "aws_elastic_beanstalk_application_version" "default" {
  name        = "tf-test-version-label"
  application = "tf-test-name"
  description = "application version created by terraform"
  bucket      = "${aws_s3_bucket.default.id}"
  key         = "${aws_s3_bucket_object.default.id}"
}

resource "aws_elastic_beanstalk_application" "tftest" {
  name = "tf-test-name"
  description = "tf-test-name"
}

resource "aws_elastic_beanstalk_environment" "tfenvtest" {
    description = "test"
    application = "${aws_elastic_beanstalk_application.tftest.name}"
    name        = "synchronicity-dev"
    cname_prefix           = "ops-api-opstest"
    solution_stack_name    = "64bit Amazon Linux 2 v5.0.1 running Node.js 12"
    tier                   = "WebServer"
    wait_for_ready_timeout = "20m"        
}

According to the official documentation, I am supplying all the Required arguments to aws_elastic_beanstalk_environment module.

However, upon executing the script, I am getting the following error:

Error waiting for Elastic Beanstalk Environment (e-39m6ygzdxh) to become ready: 2 errors occurred: * 2020-05-13 12:59:02.206 +0000 UTC (e-3xff9mzdxh) : You must specify an Instance Profile for your EC2 instance in this region. See Managing Elastic Beanstalk Instance Profiles for more information. * 2020-05-13 12:59:02.319 +0000 UTC (e-3xff9mzdxh) : Failed to launch environment.

2 Answers

This worked for me: add the setting below to your aws_elastic_beanstalk_environment resource:

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

More information on general settings here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html

Info on the aws_elastic_beanstalk_environment: https://www.terraform.io/docs/providers/aws/r/elastic_beanstalk_environment.html

resource "aws_elastic_beanstalk_application" "tftest" {
  name        = "name****"
  description = "create elastic beanstalk applications"
  tags = {
    "Name" = "name*****"
  }
}

resource "aws_elastic_beanstalk_environment" "env" {
  application         = aws_elastic_beanstalk_application.tftest.name
  solution_stack_name = "64bit Amazon Linux 2 v3.3.15 running PHP 8.0"
  
  
  
  name                = "env-application"
  tier                = "WebServer"
  setting {
    namespace = "aws:ec2:vpc"
    name      = "Vpcid"
    value     = "mention vpc id"
  }
  setting {
    namespace="aws:ec2:vpc"
    name="Subnets"
    value="mention subnet id"
  }
  setting {
      namespace = "aws:autoscaling:launchconfiguration"
      name      = "IamInstanceProfile"
      value     = "aws-elasticbeanstalk-ec2-role"
    }
}
Related