简体   繁体   中英

How to deploy Lambda using terraform created by CDKTF?

I am following this guide from hashicorp https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-assets-stacks-lambda

It uses s3 for lambda deployment package

// Create Lambda executable
    const asset = new TerraformAsset(this, "lambda-asset", {
      path: path.resolve(__dirname, config.path),
      type: AssetType.ARCHIVE, // if left empty it infers directory and file
    });

    // Create unique S3 bucket that hosts Lambda executable
    const bucket = new aws.s3Bucket.S3Bucket(this, "bucket", {
      bucketPrefix: `learn-cdktf-${name}`,
    });

    // Upload Lambda zip file to newly created S3 bucket
    const lambdaArchive = new aws.s3Object.S3Object(this, "lambda-archive", {
      bucket: bucket.bucket,
      key: `${config.version}/${asset.fileName}`,
      source: asset.path, // returns a posix path
    });
 
// Create Lambda function
const lambdaFunc = new aws.lambdaFunction.LambdaFunction(this, "learn-cdktf-lambda", {
  functionName: `learn-cdktf-${name}-${pet.id}`,
  s3Bucket: bucket.bucket,
  s3Key: lambdaArchive.key,
  handler: config.handler,
  runtime: config.runtime,
  role: role.arn
});

I have figured out a way to use the synthesised code from cdktf ( cdktf.json) in my existing terraform project, however, s3 bucket object generated uses a source which is a posit suffix

"aws_s3_object": {
      "lambda-archive": {
        "//": {
          "metadata": {
            "path": "lambda-hello-world/lambda-archive",
            "uniqueId": "lambda-archive"
          }
        },
        "bucket": "${aws_s3_bucket.bucket.bucket}",
        "key": "v0.0.2/archive.zip",
        "source": "assets/lambda-asset/ABCDEDGHIJKLAMN000006786986/archive.zip"
      }
    },

When I try to use terraform apply with cdktf.json it says source not found, how do I deal with this, is there a way to deploy lambda with cdktf without s3?

This here worked well for me. But is is using TypeScript https://github.com/cdktf/cdktf-integration-serverless-example/blob/main/lib/nodejs-function.ts

I created an example repo how it could look like in a repo https://github.com/mmuller88/cdktf-lambda .

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