AWS Beanstalk Tomcat 和 Terraform

Posted

技术标签:

【中文标题】AWS Beanstalk Tomcat 和 Terraform【英文标题】:AWS Beanstalk Tomcat and Terraform 【发布时间】:2018-11-16 05:25:40 【问题描述】:

我尝试使用 Beanstalk 设置 Tomcat。

这是我的 Terraform 代码:​​

(存储桶是预先创建的)

# 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"
  
  ...

最后我得到一个非常奇怪的错误,说它在存储桶上找不到文件。

InvalidParameterCombination:无法从 S3 位置下载 (桶:mybucket 密钥:src/java-tomcat-v3.zip)。原因:未找到

尽管如此,连接到 Web 控制台并访问我的存储桶,我可以看到 zip 文件就在那里...

没看懂,求大神帮忙?

PS:我尝试了使用和不使用src/

干杯

【问题讨论】:

这个错误是来自 Terraform 还是来自 Beanstalk? 此错误来自 Terraform。它说它不能申请,并给出了这个错误...... 【参考方案1】:

我最近在 Terraform 0.13 上遇到了同样的错误。

0.13 和旧版本的区别: 文档似乎已过时。例如,在 aws_elastic_beanstalk_application_version 下显示

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

如果您尝试使用它,terraform 会因存储桶对象而失败,因为“source”参数在 aws_elastic_beanstalk_application_version 中不再可用。 删除“源”属性后,它移至下一个问题,即Error: InvalidParameterCombination: Unable to download from S3 location (Bucket: mybucket Key: mybucket/myfile.txt). Reason: Not Found

此错误来自 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

Terraform 在这里最终做的是将存储桶添加到密钥之前。当您运行terraform plan 时,您会看到bucket = "mybucket" 和key = "mybucket/myfile.txt"。问题在于 Terraform 在存储桶中查找文件“mybucket/myfile.txt”,而它应该只查找“myfile.txt”

解决方案

我所做的是从脚本中删除存储桶和存储桶对象资源并将名称放在变量中,如下所示:

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

通过直接使用文件名和bucket,Terraform不会在key前加上bucket名,可以直接找到文件。

【讨论】:

以上是关于AWS Beanstalk Tomcat 和 Terraform的主要内容,如果未能解决你的问题,请参考以下文章

AWS Elastic Beanstalk 上的 Tomcat:公开多个端口

AWS Elastic Beanstalk:Tomcat 忽略了我的 WAR 文件

AWS Elastic Beanstalk - tomcat java spring boot 应用程序的问题

AWS Elastic Beanstalk 上带有 Tomcat 7 的 Websocket

Moqui 在 Elastic Beanstalk Tomcat 实例上部署到 AWS

AWS Elastic Beanstalk Tomcat - 内存使用率高