Skip to content

Commit d6595d0

Browse files
authored
portfolio-positions cleanup and lambda improvements (#3090)
* updated infra script * wip * wip * wip * wip * Added new type * updated dot env import * to implement * updated type * added mappers file * wip * wip * some fixes * fix lending type * wip mappers * finished getProductType * fixed undefined errors * cleanup * cleanup * added reading staging vars
1 parent 05efdbe commit d6595d0

File tree

10 files changed

+2702
-5051
lines changed

10 files changed

+2702
-5051
lines changed

infra/main.ts

+22-33
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { Construct } from 'constructs'
33
import {
44
App,
55
TerraformStack,
6-
CloudBackend,
7-
RemoteBackend,
8-
NamedCloudWorkspace,
6+
// CloudBackend,
7+
// NamedCloudWorkspace,
98
TerraformAsset,
10-
AssetType,
9+
// AssetType,
1110
TerraformOutput,
1211
} from 'cdktf'
1312

@@ -18,11 +17,10 @@ interface LambdaFunctionConfig {
1817
path: string
1918
handler: string
2019
runtime: string
21-
stageName: string
2220
version: string
2321
}
2422

25-
const lambdaRolePolicy = {
23+
const lambdaExecRolePolicy = {
2624
Version: '2012-10-17',
2725
Statement: [
2826
{
@@ -43,34 +41,34 @@ class LambdaStack extends TerraformStack {
4341
new aws.provider.AwsProvider(this, 'AWS', {
4442
region: 'eu-north-1',
4543
})
44+
new random.provider.RandomProvider(this, 'random')
4645

4746
// Create random value
4847
const pet = new random.pet.Pet(this, 'random-name', {
4948
length: 2,
5049
})
5150

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}`,
5654
})
5755

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),
6159
})
6260

6361
// Upload Lambda zip file to newly created S3 bucket
6462
const lambdaArchive = new aws.s3Object.S3Object(this, 'lambda-archive', {
6563
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,
6866
})
6967

7068
// Create Lambda role
7169
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),
7472
})
7573

7674
// Add execution role for lambda to write to CloudWatch logs
@@ -80,8 +78,8 @@ class LambdaStack extends TerraformStack {
8078
})
8179

8280
// 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}`,
8583
s3Bucket: bucket.bucket,
8684
s3Key: lambdaArchive.key,
8785
handler: config.handler,
@@ -96,7 +94,7 @@ class LambdaStack extends TerraformStack {
9694
target: lambdaFunc.arn,
9795
})
9896

99-
new aws.lambdaPermission.LambdaPermission(this, 'apigw-lambda', {
97+
new aws.lambdaPermission.LambdaPermission(this, 'api-gw-lambda-invoke', {
10098
functionName: lambdaFunc.functionName,
10199
action: 'lambda:InvokeFunction',
102100
principal: 'apigateway.amazonaws.com',
@@ -110,12 +108,11 @@ class LambdaStack extends TerraformStack {
110108
}
111109

112110
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',
116114
runtime: 'nodejs18.x',
117-
stageName: 'hello-world',
118-
version: 'v0.0.1',
115+
version: 'v0.0.7',
119116
})
120117

121118
// new CloudBackend(stack, {
@@ -124,12 +121,4 @@ const stack = new LambdaStack(app, 'lambda-hello-world', {
124121
// workspaces: new NamedCloudWorkspace('borrow-infra'),
125122
// })
126123

127-
// new RemoteBackend(stack, {
128-
// hostname: 'app.terraform.io',
129-
// organization: 'Oazo',
130-
// workspaces: {
131-
// name: 'borrow-infra',
132-
// },
133-
// })
134-
135124
app.synth()

0 commit comments

Comments
 (0)