Skip to content

Commit

Permalink
Fix: Codebuild UnitTest (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamputraintan authored Mar 6, 2024
1 parent 0540957 commit 76a9fa4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 48 deletions.
4 changes: 2 additions & 2 deletions lib/pipeline/orcabus-stateful-pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class StatefulPipelineStack extends cdk.Stack {
});

const unitTest = new pipelines.CodeBuildStep('UnitTest', {
commands: ['yarn install --frozen-lockfile', 'make test-stateful'],
commands: ['yarn install --immutable', 'make test-stateful'],
input: sourceFile,
primaryOutputDirectory: '.',
buildEnvironment: {
Expand All @@ -43,7 +43,7 @@ export class StatefulPipelineStack extends cdk.Stack {
});

const synthAction = new pipelines.CodeBuildStep('Synth', {
commands: ['yarn install --frozen-lockfile', 'yarn run cdk-stateful-pipeline synth'],
commands: ['yarn install --immutable', 'yarn run cdk-stateful-pipeline synth'],
input: unitTest,
primaryOutputDirectory: 'cdk.out',
rolePolicyStatements: [
Expand Down
74 changes: 45 additions & 29 deletions lib/pipeline/orcabus-stateless-pipeline-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export class StatelessPipelineStack extends cdk.Stack {
});

const unitTest = new pipelines.CodeBuildStep('UnitTest', {
commands: ['yarn install --frozen-lockfile', 'make suite'],
installCommands: [
// RUST installation
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y`,
`source $HOME/.cargo/env`,
`pip3 install cargo-lambda`,
],
commands: ['yarn install --immutable', 'make suite'],
input: sourceFile,
primaryOutputDirectory: '.',
buildEnvironment: {
Expand Down Expand Up @@ -47,7 +53,13 @@ export class StatelessPipelineStack extends cdk.Stack {
});

const synthAction = new pipelines.CodeBuildStep('Synth', {
commands: ['yarn install --frozen-lockfile', 'yarn run cdk-stateless-pipeline synth'],
installCommands: [
// RUST installation
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y`,
`source $HOME/.cargo/env`,
`pip3 install cargo-lambda`,
],
commands: ['yarn install --immutable', 'yarn run cdk-stateless-pipeline synth'],
input: unitTest,
primaryOutputDirectory: 'cdk.out',
rolePolicyStatements: [
Expand Down Expand Up @@ -89,34 +101,38 @@ export class StatelessPipelineStack extends cdk.Stack {
})
);

/**
* Deployment to Gamma (Staging) account
*/
const gammaConfig = getEnvironmentConfig('gamma');
if (!gammaConfig) throw new Error(`No 'Gamma' account configuration`);
pipeline.addStage(
new OrcaBusStatelessDeploymentStage(
this,
'GammaStatelessDeployment',
gammaConfig.stackProps,
{
account: gammaConfig.accountId,
}
),
{ pre: [new pipelines.ManualApprovalStep('PromoteToGamma')] }
);
// Since the stateless stack might need to reference the stateful resources (e.g. db, sg), we might comment this out
// to prevent cdk from looking up for non existence resource. Currently the stateful resource is only deployed in
// dev

/**
* Deployment to Prod account
*/
const prodConfig = getEnvironmentConfig('prod');
if (!prodConfig) throw new Error(`No 'Prod' account configuration`);
pipeline.addStage(
new OrcaBusStatelessDeploymentStage(this, 'ProdStatelessDeployment', prodConfig.stackProps, {
account: gammaConfig?.accountId,
}),
{ pre: [new pipelines.ManualApprovalStep('PromoteToProd')] }
);
// /**
// * Deployment to Gamma (Staging) account
// */
// const gammaConfig = getEnvironmentConfig('gamma');
// if (!gammaConfig) throw new Error(`No 'Gamma' account configuration`);
// pipeline.addStage(
// new OrcaBusStatelessDeploymentStage(
// this,
// 'GammaStatelessDeployment',
// gammaConfig.stackProps,
// {
// account: gammaConfig.accountId,
// }
// ),
// { pre: [new pipelines.ManualApprovalStep('PromoteToGamma')] }
// );

// /**
// * Deployment to Prod account
// */
// const prodConfig = getEnvironmentConfig('prod');
// if (!prodConfig) throw new Error(`No 'Prod' account configuration`);
// pipeline.addStage(
// new OrcaBusStatelessDeploymentStage(this, 'ProdStatelessDeployment', prodConfig.stackProps, {
// account: gammaConfig?.accountId,
// }),
// { pre: [new pipelines.ManualApprovalStep('PromoteToProd')] }
// );
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This image will generate the node_modules needed for lambda and let
# the layer reference the output from this image

FROM public.ecr.aws/docker/library/node:20-alpine

WORKDIR /home/node/app

COPY package.json package.json
COPY yarn.lock yarn.lock
RUN yarn install --immutable

# making nodejs folder where as guided in the AWS lambda layer docs
RUN mkdir -p output/nodejs

# copy node_modules to that folder
RUN cp -r node_modules output/nodejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as iam from 'aws-cdk-lib/aws-iam';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import { MicroserviceConfig, DbAuthType } from '../function/type';
import package_json from '../package.json';

export type PostgresManagerConfig = {
masterSecretName: string;
Expand Down Expand Up @@ -38,9 +39,21 @@ export class PostgresManagerStack extends Stack {
props.clusterResourceIdParameterName
);

const rdsLambdaProps : nodejs.NodejsFunctionProps = {
const dependencyLayer = new lambda.LayerVersion(this, 'DependenciesLayer', {
code: lambda.Code.fromDockerBuild(__dirname + '/../', {
cacheDisabled: true,
file: 'deploy/construct/layer/node_module.Dockerfile',
imagePath: 'home/node/app/output',
}),
compatibleArchitectures: [lambda.Architecture.ARM_64],
compatibleRuntimes: [lambda.Runtime.NODEJS_20_X],
});

const runtimeDependencies = Object.keys(package_json.dependencies);
const rdsLambdaProps: nodejs.NodejsFunctionProps = {
layers: [dependencyLayer],
bundling: { externalModules: runtimeDependencies },
timeout: Duration.minutes(5),
depsLockFilePath: __dirname + '/../yarn.lock',
handler: 'handler',
runtime: lambda.Runtime.NODEJS_20_X,
architecture: lambda.Architecture.ARM_64,
Expand Down Expand Up @@ -98,9 +111,7 @@ export class PostgresManagerStack extends Stack {
new iam.PolicyStatement({
actions: ['secretsmanager:CreateSecret', 'secretsmanager:TagResource'],
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:secretsmanager:ap-southeast-2:${process.env.CDK_DEFAULT_ACCOUNT}:secret:*`,
],
resources: [`arn:aws:secretsmanager:ap-southeast-2:*:secret:*`],
}),
new iam.PolicyStatement({
actions: ['secretsmanager:GetRandomPassword'],
Expand Down
24 changes: 12 additions & 12 deletions lib/workload/stateless/postgres_manager/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1042,18 +1042,6 @@ __metadata:
languageName: node
linkType: hard

"lambda-with-rds@workspace:.":
version: 0.0.0-use.local
resolution: "lambda-with-rds@workspace:."
dependencies:
"@aws-sdk/client-secrets-manager": ^3.515.0
"@types/aws-lambda": ^8.10.134
"@types/pg": ^8
pg: ^8.11.3
typescript: ^5.3.3
languageName: unknown
linkType: soft

"obuf@npm:~1.1.2":
version: 1.1.2
resolution: "obuf@npm:1.1.2"
Expand Down Expand Up @@ -1233,6 +1221,18 @@ __metadata:
languageName: node
linkType: hard

"postgres-manager@workspace:.":
version: 0.0.0-use.local
resolution: "postgres-manager@workspace:."
dependencies:
"@aws-sdk/client-secrets-manager": ^3.515.0
"@types/aws-lambda": ^8.10.134
"@types/pg": ^8
pg: ^8.11.3
typescript: ^5.3.3
languageName: unknown
linkType: soft

"postgres-range@npm:^1.1.1":
version: 1.1.4
resolution: "postgres-range@npm:1.1.4"
Expand Down

0 comments on commit 76a9fa4

Please sign in to comment.