Skip to content

Commit accfbb8

Browse files
authored
fix(cdk): use default VPC (#19)
Because we do not need a custom VPC, the default can be used. Fixes #17
1 parent b5dd9d1 commit accfbb8

File tree

7 files changed

+55
-22
lines changed

7 files changed

+55
-22
lines changed

cdk/backend.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ECRClient } from '@aws-sdk/client-ecr'
22
import { IAMClient } from '@aws-sdk/client-iam'
33
import { IoTClient } from '@aws-sdk/client-iot'
4-
import { GetCallerIdentityCommand, STS } from '@aws-sdk/client-sts'
4+
import { STS } from '@aws-sdk/client-sts'
55
import path from 'node:path'
66
import { getIoTEndpoint } from '../aws/getIoTEndpoint.js'
77
import { getOrBuildDockerImage } from '../aws/getOrBuildDockerImage.js'
@@ -12,6 +12,7 @@ import { debug } from '../cli/log.js'
1212
import pJSON from '../package.json'
1313
import { BackendApp } from './BackendApp.js'
1414
import { ensureGitHubOIDCProvider } from './ensureGitHubOIDCProvider.js'
15+
import { env } from './helpers/env.js'
1516
import { packLayer } from './helpers/lambdas/packLayer.js'
1617
import { packBackendLambdas } from './packBackendLambdas.js'
1718
import { ECR_NAME } from './stacks/stackConfig.js'
@@ -27,6 +28,8 @@ const sts = new STS({})
2728
const ecr = new ECRClient({})
2829
const iam = new IAMClient({})
2930

31+
const accountEnv = await env({ sts })
32+
3033
const packagesInLayer: string[] = [
3134
'@nordicsemiconductor/from-env',
3235
'@nordicsemiconductor/timestream-helpers',
@@ -38,9 +41,7 @@ const packagesInLayer: string[] = [
3841
'lodash-es',
3942
'@middy/core',
4043
]
41-
const accountId = (await sts.send(new GetCallerIdentityCommand({})))
42-
.Account as string
43-
const certsDir = path.join(process.cwd(), 'certificates', accountId)
44+
const certsDir = path.join(process.cwd(), 'certificates', accountEnv.account)
4445
const mqttBridgeCertificate = await ensureMQTTBridgeCredentials({
4546
iot,
4647
certsDir,
@@ -86,10 +87,9 @@ new BackendApp({
8687
imageTag,
8788
repositoryUri,
8889
},
89-
region:
90-
process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION ?? 'eu-west-1',
9190
repository,
9291
gitHubOICDProviderArn: await ensureGitHubOIDCProvider({
9392
iam,
9493
}),
94+
env: accountEnv,
9595
})

cdk/helpers/env.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts'
2+
import type { Environment } from 'aws-cdk-lib'
3+
4+
export const env = async ({
5+
sts,
6+
}: {
7+
sts: STSClient
8+
}): Promise<Required<Environment>> => {
9+
const { Account } = await sts.send(new GetCallerIdentityCommand({}))
10+
if (Account === undefined) throw new Error(`Failed to get caller identity!`)
11+
return {
12+
account: Account,
13+
region:
14+
process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION ?? 'eu-west-1',
15+
}
16+
}

cdk/resources/Integration.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
aws_iot as IoT,
66
Stack,
77
} from 'aws-cdk-lib'
8-
import type { IVpc } from 'aws-cdk-lib/aws-ec2'
98
import type { IRepository } from 'aws-cdk-lib/aws-ecr'
109
import { LogDriver, type ICluster } from 'aws-cdk-lib/aws-ecs'
1110
import { RetentionDays } from 'aws-cdk-lib/aws-logs'
@@ -132,12 +131,10 @@ export class Integration extends Construct {
132131
principal: this.bridgeCertificate.attrArn,
133132
})
134133

135-
const vpc = new EC2.Vpc(this, `vpc`, {
136-
maxAzs: 1,
137-
})
134+
const vpc = EC2.Vpc.fromLookup(this, 'DefaultVPC', { isDefault: true })
138135

139136
const cluster = new ECS.Cluster(this, `cluster`, {
140-
vpc: vpc as IVpc,
137+
vpc,
141138
})
142139

143140
const mqttBridgeTask = new ECS.FargateTaskDefinition(this, 'mqttBridge')
@@ -270,7 +267,8 @@ export class Integration extends Construct {
270267
cluster: cluster as ICluster,
271268
taskDefinition: mqttBridgeTask,
272269
desiredCount: 1,
273-
assignPublicIp: this.node.tryGetContext('isTest') ?? false,
270+
// Required for shared VPC and access to SSM Parameters
271+
assignPublicIp: true,
274272
},
275273
)
276274
// Add inbound port to security group

cdk/stacks/BackendStack.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { App, CfnOutput, aws_lambda as Lambda, Stack } from 'aws-cdk-lib'
1+
import {
2+
App,
3+
CfnOutput,
4+
aws_lambda as Lambda,
5+
Stack,
6+
type Environment,
7+
} from 'aws-cdk-lib'
28
import { type CAFiles } from '../../bridge/caLocation.js'
39
import type { CertificateFiles } from '../../bridge/mqttBridgeCertificateLocation.js'
410
import type { BackendLambdas } from '../BackendLambdas.js'
@@ -26,28 +32,26 @@ export class BackendStack extends Stack {
2632
mqttBridgeCertificate,
2733
caCertificate,
2834
bridgeImageSettings,
29-
region,
3035
repository,
3136
gitHubOICDProviderArn,
37+
env,
3238
}: {
3339
lambdaSources: BackendLambdas
3440
layer: PackedLayer
3541
iotEndpoint: string
3642
mqttBridgeCertificate: CertificateFiles
3743
caCertificate: CAFiles
3844
bridgeImageSettings: BridgeImageSettings
39-
region: string
4045
gitHubOICDProviderArn: string
4146
repository: {
4247
owner: string
4348
repo: string
4449
}
50+
env: Required<Environment>
4551
},
4652
) {
4753
super(parent, STACK_NAME, {
48-
env: {
49-
region,
50-
},
54+
env,
5155
})
5256

5357
const baseLayer = new Lambda.LayerVersion(this, 'baseLayer', {

cdk/test-resources.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { STSClient } from '@aws-sdk/client-sts'
2+
import { env } from './helpers/env.js'
13
import { packLambdaFromPath } from './helpers/lambdas/packLambdaFromPath.js'
24
import { packLayer } from './helpers/lambdas/packLayer.js'
35
import { TestResources } from './test-resources/TestResourcesApp.js'
46

7+
const awsEnv = await env({ sts: new STSClient({}) })
8+
59
new TestResources({
610
lambdaSources: {
711
httpApiMock: await packLambdaFromPath(
@@ -13,4 +17,5 @@ new TestResources({
1317
id: 'testResources',
1418
dependencies: ['@aws-sdk/client-dynamodb', '@nordicsemiconductor/from-env'],
1519
}),
20+
env: awsEnv,
1621
})
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App } from 'aws-cdk-lib'
1+
import { App, type Environment } from 'aws-cdk-lib'
22
import type { PackedLambda } from '../helpers/lambdas/packLambda'
33
import type { PackedLayer } from '../helpers/lambdas/packLayer'
44
import { TestResourcesStack } from './TestResourcesStack.js'
@@ -8,14 +8,16 @@ export class TestResources extends App {
88
lambdaSources,
99
context,
1010
layer,
11+
env,
1112
}: {
1213
lambdaSources: {
1314
httpApiMock: PackedLambda
1415
}
1516
layer: PackedLayer
1617
context?: Record<string, any>
18+
env: Required<Environment>
1719
}) {
1820
super({ context })
19-
new TestResourcesStack(this, { lambdaSources, layer })
21+
new TestResourcesStack(this, { lambdaSources, layer, env })
2022
}
2123
}

cdk/test-resources/TestResourcesStack.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { App, CfnOutput, aws_lambda as Lambda, Stack } from 'aws-cdk-lib'
1+
import {
2+
App,
3+
CfnOutput,
4+
aws_lambda as Lambda,
5+
Stack,
6+
type Environment,
7+
} from 'aws-cdk-lib'
28
import type { PackedLambda } from '../helpers/lambdas/packLambda.js'
39
import type { PackedLayer } from '../helpers/lambdas/packLayer.js'
410
import { TEST_RESOURCES_STACK_NAME } from '../stacks/stackConfig.js'
@@ -13,14 +19,16 @@ export class TestResourcesStack extends Stack {
1319
{
1420
lambdaSources,
1521
layer,
22+
env,
1623
}: {
1724
lambdaSources: {
1825
httpApiMock: PackedLambda
1926
}
2027
layer: PackedLayer
28+
env: Required<Environment>
2129
},
2230
) {
23-
super(parent, TEST_RESOURCES_STACK_NAME)
31+
super(parent, TEST_RESOURCES_STACK_NAME, { env })
2432

2533
const baseLayer = new Lambda.LayerVersion(this, 'baseLayer', {
2634
code: Lambda.Code.fromAsset(layer.layerZipFile),

0 commit comments

Comments
 (0)