0

我正在将我的 terraform 应用程序移动到 GH Action 中。我正在按照本指南设置 GH 操作。我已经创建了 terraform HCL,但现在我的 Lambda 层无法上传到 S3 存储桶。如何将文件获取到远程 terraform?

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.28.0"
    }
  }
  required_version = "~> 0.14"

  backend "remote" {
    organization = "tnorlund"

    workspaces {
      name = "gh-actions-demo"
    }
  }
}
...
module "python_layer" {
  source      = "./LambdaLayer"
  type        = "python"
  path        = ".."
  developer   = "Tyler Norlund"
  bucket_name = module.layer_bucket.bucket_name
  stage       = var.stage
}
...
#LambdaLayer/main.tf
# Adds a NodeJS or Python Lambda Layer

# Upload the compressed code to the S3 bucket
resource "aws_s3_bucket_object" "object" {
  bucket = var.bucket_name
  key    = var.type == "nodejs" ? "nodejs.zip" : "python.zip"
  source = var.type == "nodejs" ? "${var.path}/nodejs.zip" : "${var.path}/python.zip"
  etag   = var.type == "nodejs" ? filemd5("${var.path}/nodejs.zip") : filemd5("${var.path}/python.zip")
  tags = {
    Project   = "Blog"
    Stage     = var.stage
    Developer = var.developer
  }
}

# Use the uploaded code as the Lambda Layer's code
resource "aws_lambda_layer_version" "layer" {
  layer_name = var.type == "nodejs" ? "analytics_js" : "analytics_python"
  s3_bucket = var.bucket_name
  s3_key = aws_s3_bucket_object.object.key

  description = var.type == "nodejs" ? "Node Framework used to access DynamoDB" : "Python Framework used to access DynamoDB"
  compatible_runtimes = var.type == "nodejs" ? ["nodejs12.x"] : ["python3.8"]
  source_code_hash = var.type == "nodejs" ? filebase64sha256("${var.path}/nodejs.zip") : filebase64sha256("${var.path}/python.zip")
}
4

0 回答 0