Skip to content

Commit

Permalink
portfolio-positions cleanup and lambda improvements (#3090)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
piotrwitek authored Nov 9, 2023
1 parent 05efdbe commit d6595d0
Show file tree
Hide file tree
Showing 10 changed files with 2,702 additions and 5,051 deletions.
55 changes: 22 additions & 33 deletions infra/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { Construct } from 'constructs'
import {
App,
TerraformStack,
CloudBackend,
RemoteBackend,
NamedCloudWorkspace,
// CloudBackend,
// NamedCloudWorkspace,
TerraformAsset,
AssetType,
// AssetType,
TerraformOutput,
} from 'cdktf'

Expand All @@ -18,11 +17,10 @@ interface LambdaFunctionConfig {
path: string
handler: string
runtime: string
stageName: string
version: string
}

const lambdaRolePolicy = {
const lambdaExecRolePolicy = {
Version: '2012-10-17',
Statement: [
{
Expand All @@ -43,34 +41,34 @@ class LambdaStack extends TerraformStack {
new aws.provider.AwsProvider(this, 'AWS', {
region: 'eu-north-1',
})
new random.provider.RandomProvider(this, 'random')

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

// 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 artifacts
const bucket = new aws.s3Bucket.S3Bucket(this, 'artifacts', {
bucketPrefix: `artifacts-${name}`,
})

// Create unique S3 bucket that hosts Lambda executable
const bucket = new aws.s3Bucket.S3Bucket(this, 'bucket', {
bucketPrefix: `learn-cdktf-${name}`,
// Create Lambda asset
const lambdaAsset = new TerraformAsset(this, 'lambda-asset', {
path: path.resolve(__dirname, config.path),
})

// 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
key: `${pet.id}/${lambdaAsset.fileName}`,
source: lambdaAsset.path,
})

// Create Lambda role
const role = new aws.iamRole.IamRole(this, 'lambda-exec', {
name: `learn-cdktf-${name}-${pet.id}`,
assumeRolePolicy: JSON.stringify(lambdaRolePolicy),
name: `lambda-exec-${name}-${pet.id}`,
assumeRolePolicy: JSON.stringify(lambdaExecRolePolicy),
})

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

// Create Lambda function
const lambdaFunc = new aws.lambdaFunction.LambdaFunction(this, 'learn-cdktf-lambda', {
functionName: `learn-cdktf-${name}-${pet.id}`,
const lambdaFunc = new aws.lambdaFunction.LambdaFunction(this, 'lambda', {
functionName: `${name}-${pet.id}`,
s3Bucket: bucket.bucket,
s3Key: lambdaArchive.key,
handler: config.handler,
Expand All @@ -96,7 +94,7 @@ class LambdaStack extends TerraformStack {
target: lambdaFunc.arn,
})

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

const app = new App()
const stack = new LambdaStack(app, 'lambda-hello-world', {
path: '../lambdas/dist/portfolio-overview',
handler: 'index.handler',
new LambdaStack(app, 'portfolio-positions-2', {
path: '../lambdas/artifacts/portfolio-positions.zip',
handler: 'dist/portfolio-positions/index.handler',
runtime: 'nodejs18.x',
stageName: 'hello-world',
version: 'v0.0.1',
version: 'v0.0.7',
})

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

// new RemoteBackend(stack, {
// hostname: 'app.terraform.io',
// organization: 'Oazo',
// workspaces: {
// name: 'borrow-infra',
// },
// })

app.synth()
Loading

0 comments on commit d6595d0

Please sign in to comment.