@@ -3,11 +3,10 @@ import { Construct } from 'constructs'
3
3
import {
4
4
App ,
5
5
TerraformStack ,
6
- CloudBackend ,
7
- RemoteBackend ,
8
- NamedCloudWorkspace ,
6
+ // CloudBackend,
7
+ // NamedCloudWorkspace,
9
8
TerraformAsset ,
10
- AssetType ,
9
+ // AssetType,
11
10
TerraformOutput ,
12
11
} from 'cdktf'
13
12
@@ -18,11 +17,10 @@ interface LambdaFunctionConfig {
18
17
path : string
19
18
handler : string
20
19
runtime : string
21
- stageName : string
22
20
version : string
23
21
}
24
22
25
- const lambdaRolePolicy = {
23
+ const lambdaExecRolePolicy = {
26
24
Version : '2012-10-17' ,
27
25
Statement : [
28
26
{
@@ -43,34 +41,34 @@ class LambdaStack extends TerraformStack {
43
41
new aws . provider . AwsProvider ( this , 'AWS' , {
44
42
region : 'eu-north-1' ,
45
43
} )
44
+ new random . provider . RandomProvider ( this , 'random' )
46
45
47
46
// Create random value
48
47
const pet = new random . pet . Pet ( this , 'random-name' , {
49
48
length : 2 ,
50
49
} )
51
50
52
- // Create Lambda executable
53
- const asset = new TerraformAsset ( this , 'lambda-asset' , {
54
- path : path . resolve ( __dirname , config . path ) ,
55
- type : AssetType . ARCHIVE , // if left empty it infers directory and file
51
+ // Create unique S3 bucket that hosts artifacts
52
+ const bucket = new aws . s3Bucket . S3Bucket ( this , 'artifacts' , {
53
+ bucketPrefix : `artifacts-${ name } ` ,
56
54
} )
57
55
58
- // Create unique S3 bucket that hosts Lambda executable
59
- const bucket = new aws . s3Bucket . S3Bucket ( this , 'bucket ' , {
60
- bucketPrefix : `learn-cdktf- ${ name } ` ,
56
+ // Create Lambda asset
57
+ const lambdaAsset = new TerraformAsset ( this , 'lambda-asset ' , {
58
+ path : path . resolve ( __dirname , config . path ) ,
61
59
} )
62
60
63
61
// Upload Lambda zip file to newly created S3 bucket
64
62
const lambdaArchive = new aws . s3Object . S3Object ( this , 'lambda-archive' , {
65
63
bucket : bucket . bucket ,
66
- key : `${ config . version } /${ asset . fileName } ` ,
67
- source : asset . path , // returns a posix path
64
+ key : `${ pet . id } /${ lambdaAsset . fileName } ` ,
65
+ source : lambdaAsset . path ,
68
66
} )
69
67
70
68
// Create Lambda role
71
69
const role = new aws . iamRole . IamRole ( this , 'lambda-exec' , {
72
- name : `learn-cdktf -${ name } -${ pet . id } ` ,
73
- assumeRolePolicy : JSON . stringify ( lambdaRolePolicy ) ,
70
+ name : `lambda-exec -${ name } -${ pet . id } ` ,
71
+ assumeRolePolicy : JSON . stringify ( lambdaExecRolePolicy ) ,
74
72
} )
75
73
76
74
// Add execution role for lambda to write to CloudWatch logs
@@ -80,8 +78,8 @@ class LambdaStack extends TerraformStack {
80
78
} )
81
79
82
80
// Create Lambda function
83
- const lambdaFunc = new aws . lambdaFunction . LambdaFunction ( this , 'learn-cdktf- lambda' , {
84
- functionName : `learn-cdktf- ${ name } -${ pet . id } ` ,
81
+ const lambdaFunc = new aws . lambdaFunction . LambdaFunction ( this , 'lambda' , {
82
+ functionName : `${ name } -${ pet . id } ` ,
85
83
s3Bucket : bucket . bucket ,
86
84
s3Key : lambdaArchive . key ,
87
85
handler : config . handler ,
@@ -96,7 +94,7 @@ class LambdaStack extends TerraformStack {
96
94
target : lambdaFunc . arn ,
97
95
} )
98
96
99
- new aws . lambdaPermission . LambdaPermission ( this , 'apigw- lambda' , {
97
+ new aws . lambdaPermission . LambdaPermission ( this , 'api-gw- lambda-invoke ' , {
100
98
functionName : lambdaFunc . functionName ,
101
99
action : 'lambda:InvokeFunction' ,
102
100
principal : 'apigateway.amazonaws.com' ,
@@ -110,12 +108,11 @@ class LambdaStack extends TerraformStack {
110
108
}
111
109
112
110
const app = new App ( )
113
- const stack = new LambdaStack ( app , 'lambda-hello-world ' , {
114
- path : '../lambdas/dist /portfolio-overview ' ,
115
- handler : 'index.handler' ,
111
+ new LambdaStack ( app , 'portfolio-positions-2 ' , {
112
+ path : '../lambdas/artifacts /portfolio-positions.zip ' ,
113
+ handler : 'dist/portfolio-positions/ index.handler' ,
116
114
runtime : 'nodejs18.x' ,
117
- stageName : 'hello-world' ,
118
- version : 'v0.0.1' ,
115
+ version : 'v0.0.7' ,
119
116
} )
120
117
121
118
// new CloudBackend(stack, {
@@ -124,12 +121,4 @@ const stack = new LambdaStack(app, 'lambda-hello-world', {
124
121
// workspaces: new NamedCloudWorkspace('borrow-infra'),
125
122
// })
126
123
127
- // new RemoteBackend(stack, {
128
- // hostname: 'app.terraform.io',
129
- // organization: 'Oazo',
130
- // workspaces: {
131
- // name: 'borrow-infra',
132
- // },
133
- // })
134
-
135
124
app . synth ( )
0 commit comments