Reputation: 11598
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 ?
Upvotes: 1
Views: 1138
Reputation: 11
If your Lambda code is stored locally, you can use the filename
argument to deploy a Lambda function from Terraform/CDKTF using the local artifact without using S3:
Once you have created your deployment package you can specify it either directly as a local file (using the filename argument) or indirectly via Amazon S3 (using the s3_bucket, s3_key and s3_object_version arguments). When providing the deployment package via S3 it may be useful to use the aws_s3_object resource to upload it.
Otherwise, you can try using an absolute path when creating the asset
Assets support both absolute and relative paths. Relative paths are always considered to be relative to your project's cdktf.json file.
Upvotes: 0
Reputation: 204
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 .
Upvotes: 0