This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created a working version of the Lambda function * Created a zip file of the Lambda code and uploaded to the S3 code bucket * Added a CloudWatch Group and IAM role to be used by the Lambda function * Added an IAM policy for logging to CloudWatch and attached to the IAM Role * Enabled versioning on the S3 code bucket * Ended up having to switch from using a SourceCodeHash to the S3 version... * Tried to use both the asset and source hash of both the zip file as well as the uploaded S3 object and both were causing a continuous update of the function (hashicorp/terraform-provider-aws#7385) * Tried to manually create the base64 encoded hash from the Lambda code zip, but CDKTF was trying to access the zip before it was actually created * Seems like this is a better solution anyway, as the S3 bucket for the code should probably be versioned anyway
- Loading branch information
1 parent
cf5946c
commit ab711fb
Showing
5 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,111 @@ | ||
import { TerraformStack } from "cdktf"; | ||
import { AssetType, TerraformAsset, TerraformStack } from "cdktf"; | ||
import { Construct } from "constructs"; | ||
import { AwsProvider } from "@cdktf/provider-aws"; | ||
import { AwsProvider, s3, cloudwatch, iam, lambdafunction } from "@cdktf/provider-aws"; | ||
import * as s3Lib from "../lib/s3Bucket"; | ||
import path = require("path"); | ||
|
||
export interface LambdaProps { | ||
readonly projectName: string, | ||
} | ||
|
||
const lambdaAssumePolicy = { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
|
||
const lambdaCloudWatchPolicy = { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
], | ||
"Resource": "arn:aws:logs:*:*:*", | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
|
||
const lambdaExecutionPolicyArn = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' | ||
|
||
export class LambdaStack extends TerraformStack { | ||
constructor(scope: Construct, name: string, props: LambdaProps) { | ||
super(scope, name); | ||
|
||
new AwsProvider(this, 'aws', {}); | ||
|
||
s3Lib.createBucket(this, props.projectName + "-code", true); | ||
const lambdaName = `${props.projectName}-lambda` | ||
|
||
const codeBucket = s3Lib.createBucket( | ||
this, | ||
props.projectName + "-code", | ||
true, | ||
true | ||
); | ||
|
||
const lambdaZip = new TerraformAsset(this, "lambdaZip", { | ||
path: path.resolve("../lambda"), | ||
type: AssetType.ARCHIVE | ||
}); | ||
|
||
const lambdaZipUpload = new s3.S3Object(this, "lambdaZipUpload", { | ||
bucket: codeBucket.bucket, | ||
key: "lambda.zip", | ||
source: lambdaZip.path, | ||
sourceHash: lambdaZip.assetHash | ||
}); | ||
|
||
const logGroup = new cloudwatch.CloudwatchLogGroup(this, "lambdaCloudWatchGroup", { | ||
name: `/aws/lambda/${lambdaName}`, | ||
retentionInDays: 7 | ||
}); | ||
|
||
const lambdaRole = new iam.IamRole(this, "lambdaRole", { | ||
name: `${props.projectName}-lambda`, | ||
assumeRolePolicy: JSON.stringify(lambdaAssumePolicy) | ||
}); | ||
|
||
const lambdaLoggingPolicy = new iam.IamPolicy(this, "lambdaLoggingPolicy", { | ||
name: `${props.projectName}-lambda-logging`, | ||
path: "/", | ||
description: "Policy to allow the IAM role for Lambda to write to CloudWatch Logs", | ||
policy: JSON.stringify(lambdaCloudWatchPolicy) | ||
}); | ||
|
||
const loggingPolicyAttachment = new iam.IamRolePolicyAttachment(this, "loggingPolicyAttachment", { | ||
policyArn: lambdaLoggingPolicy.arn, | ||
role: lambdaRole.name | ||
}); | ||
|
||
new iam.IamRolePolicyAttachment(this, "executionPolicyAttachment", { | ||
policyArn: lambdaExecutionPolicyArn, | ||
role: lambdaRole.name | ||
}); | ||
|
||
new lambdafunction.LambdaFunction(this, "lambdaFunction", { | ||
functionName: lambdaName, | ||
role: lambdaRole.arn, | ||
s3Bucket: codeBucket.bucket, | ||
s3Key: lambdaZipUpload.key, | ||
s3ObjectVersion: lambdaZipUpload.versionId, | ||
// TODO Make it dynamic | ||
handler: "index.handler", | ||
// TODO Make this dynamic as well | ||
runtime: "nodejs16.x", | ||
dependsOn: [ | ||
logGroup, | ||
loggingPolicyAttachment, | ||
], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters