简体   繁体   中英

AWS Beanstalk Tomcat and Terraform

I try to set up a Tomcat using Beanstalk.

Here's my Terraform code:

(bucket is created beforehand)

# Upload the JAR to bucket
resource "aws_s3_bucket_object" "myjar" {
  bucket = "${aws_s3_bucket.mybucket.id}"
  key    = "src/java-tomcat-v3.zip"
  source = "${path.module}/src/java-tomcat-v3.zip"
  etag   = "${md5(file("${path.module}/src/java-tomcat-v3.zip"))}"
}

# Define app
resource "aws_elastic_beanstalk_application" "tftestapp" {
  name        = "tf-test-name"
  description = "tf-test-desc"
}

# Define beanstalk jar version
resource "aws_elastic_beanstalk_application_version" "myjarversion" {
  name         = "tf-test-version-label"
  application  = "tf-test-name"
  description  = "My description"
  bucket       = "${aws_s3_bucket.mybucket.id}"
  key          = "${aws_s3_bucket_object.myjar.id}"
  force_delete = true
}

# Deploy env
resource "aws_elastic_beanstalk_environment" "tftestenv" {
  name                = "tf-test-name"
  application         = "${aws_elastic_beanstalk_application.tftestapp.name}"
  solution_stack_name = "64bit Amazon Linux 2018.03 v3.0.0 running Tomcat 7 Java 7"

  setting {
    namespace = "aws:autoscaling:asg"
    name      = "MinSize"
    value     = "1"
  }
  ...
}

And I end up with a very strange error, saying it can't find the file on the bucket.

InvalidParameterCombination: Unable to download from S3 location (Bucket: mybucket Key: src/java-tomcat-v3.zip). Reason: Not Found

Nevertheless, connecting to the web console and accessing my bucket, I can see the zip file is right there...

I don't get it, any help please?

PS: I tried with and without the src/

Cheers

I was recently having this same error on Terraform 0.13.

Differences between 0.13 and older versions: The documentation appears to be out of date. For instance, under aws_elastic_beanstalk_application_version it shows

resource "aws_s3_bucket" "default" {
  bucket = "tftest.applicationversion.bucket"
}
resource "aws_s3_bucket_object" "default" {
  bucket = aws_s3_bucket.default.id
  key    = "beanstalk/go-v1.zip"
  source = "go-v1.zip"
}
resource "aws_elastic_beanstalk_application" "default" {
  name        = "tf-test-name"
  description = "tf-test-desc"
}
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
}

If you attempt to use this, terraform fails with the bucket object because the "source" argument is no longer available within aws_elastic_beanstalk_application_version. After removing the "source" property, it moved to the next issue, which was Error: InvalidParameterCombination: Unable to download from S3 location (Bucket: mybucket Key: mybucket/myfile.txt). Reason: Not Found Error: InvalidParameterCombination: Unable to download from S3 location (Bucket: mybucket Key: mybucket/myfile.txt). Reason: Not Found

This error comes from the terraform:

resource "aws_s3_bucket" "bucket" {
  bucket = "mybucket"
}
resource "aws_s3_bucket_object" "default" {
  bucket = aws_s3_bucket.bucket.id
  key    = "myfile.txt"
}
resource "aws_elastic_beanstalk_application" "default" {
  name        = "tf-test-name"
  description = "tf-test-desc"
}
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.bucket.id
  key         = aws_s3_bucket_object.default.id
}

What Terraform ends up doing here is it prepends the bucket to the key. When you run terraform plan you see that bucket = "mybucket" and key = "mybucket/myfile.txt". The problem with this is that Terraform looks in the bucket for the file "mybucket/myfile.txt" when it should ONLY be looking for "myfile.txt"

Solution

What I did was REMOVE the bucket and bucket object resources from the script and place the names in variables, as follows:

variable "sourceCodeS3BucketName" {
   type = string
   description = "The bucket that contains the engine code."
   default = "mybucket"
}
variable "sourceCodeFilename" {
   type = string
   description = "The code file name."
   default = "myfile.txt"
}
resource "aws_elastic_beanstalk_application" "myApp" {
  name        = "my-beanstalk-app"
  description = "My application"
}
resource "aws_elastic_beanstalk_application_version" "v1_0_0" {
  name        = "my-application-v1_0_0"
  application = aws_elastic_beanstalk_application.myApp.name
  description = "Application v1.0.0"
  bucket      = var.sourceCodeS3BucketName
  key         = var.sourceCodeFilename
}

By directly using the name of the file and the bucket, Terraform does not prepend the bucket name to the key, and it can find the file just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM