Skip to content

Commit dfe33ee

Browse files
committed
feat: update codepipeline to V2
1 parent 586e9bd commit dfe33ee

File tree

8 files changed

+354
-216
lines changed

8 files changed

+354
-216
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,45 +49,46 @@ npm run lint && npm run test
4949

5050
1. Download and run docker image of dynamodb-local
5151

52-
```shell
53-
docker pull amazon/dynamodb-local
54-
docker run -p 8000:8000 amazon/dynamodb-local
55-
```
52+
```shell
53+
docker pull amazon/dynamodb-local
54+
docker run -p 8000:8000 amazon/dynamodb-local
55+
```
5656

5757
2. Create dynamodb-local tables - review the template.yaml because the ID is going to change
5858

59-
```shell
60-
aws dynamodb create-table --table-name bloginfrastructuresubscriptiondbF44CF6DE --attribute-definitions AttributeName=email,AttributeType=S --key-schema AttributeName=email,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
61-
aws dynamodb create-table --table-name bloginfrastructurecontactdbA1E13C61 --attribute-definitions AttributeName=name,AttributeType=S --key-schema AttributeName=name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
62-
```
59+
```shell
60+
aws dynamodb create-table --table-name bloginfrastructuresubscriptiondbF44CF6DE --attribute-definitions AttributeName=email,AttributeType=S --key-schema AttributeName=email,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
61+
aws dynamodb create-table --table-name bloginfrastructurecontactdbA1E13C61 --attribute-definitions AttributeName=name,AttributeType=S --key-schema AttributeName=name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --endpoint-url http://localhost:8000
62+
```
6363

6464
3. Update lambda code to change the endpoint:
6565

66-
```shell
67-
const dbClient = process.env.AWS_SAM_LOCAL ? new DynamoDBClient({ endpoint: 'http://docker.for.mac.localhost:8000/', }) : new DynamoDBClient();
68-
```
66+
```shell
67+
const dbClient = process.env.AWS_SAM_LOCAL ? new DynamoDBClient({ endpoint: 'http://docker.for.mac.localhost:8000/', }) : new DynamoDBClient();
68+
```
6969

7070
4. Generate the YAML file that SAM needs
7171

72-
```shell
73-
cdk synth blog-backend-infrastructure --no-staging > template.yml
74-
```
72+
```shell
73+
cdk synth blog-backend-infrastructure --no-staging > template.yml
74+
```
7575

7676
5. Run SAM tests
7777

78-
```shell
79-
sam local invoke blog-backend-infrastructure-contact-lbd -e tests/events/contact.json
80-
sam local invoke blog-backend-infrastructure-subscription-lbd -e tests/events/subscription.json
81-
```
78+
```shell
79+
sam local invoke blog-backend-infrastructure-contact-lbd -e tests/events/contact.json
80+
sam local invoke blog-backend-infrastructure-subscription-lbd -e tests/events/subscription.json
81+
```
8282

8383
6. Review database records
8484

85-
```shell
86-
aws dynamodb scan --table-name bloginfrastructuresubscriptiondbF44CF6DE --endpoint-url http://localhost:8000
87-
aws dynamodb scan --table-name bloginfrastructurecontactdbA1E13C61 --endpoint-url http://localhost:8000
88-
```
85+
```shell
86+
aws dynamodb scan --table-name bloginfrastructuresubscriptiondbF44CF6DE --endpoint-url http://localhost:8000
87+
aws dynamodb scan --table-name bloginfrastructurecontactdbA1E13C61 --endpoint-url http://localhost:8000
88+
```
89+
90+
### Additional links
8991

90-
### Additional links
9192
- How to create infrastructure with CDK [here](https://www.playingaws.com/posts/how-to-create-infrastructure-with-cdk/).
9293
- How to add CI/CD to my CDK project [here](https://www.playingaws.com/posts/how-to-add-ci-cd-to-my-cdk-project/).
9394
- How to create serverless applications with CDK and SAM [here](https://www.playingaws.com/posts/how-to-create-serverless-applications-with-cdk-and-sam/)

backend/events/eventbridge.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// import { Construct } from 'constructs';
2+
// import { aws_dynamodb, aws_sns as sns } from 'aws-cdk-lib';
3+
// import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
4+
// import { CfnPipe } from 'aws-cdk-lib/aws-pipes';
5+
6+
// // export function createTopicMyNotification(scope: Construct): sns.Topic {
7+
// // const topicName = `${config.get('name')}` + `-alarms`;
8+
// // const myKeyAlias = kms.Alias.fromAliasName(
9+
// // scope,
10+
// // 'defaultKey',
11+
// // 'alias/aws/sns',
12+
// // );
13+
// // const topic = new sns.Topic(scope, topicName + '-topic', {
14+
// // topicName: topicName,
15+
// // displayName: 'Topic to deliver notifications to myself',
16+
// // masterKey: myKeyAlias,
17+
// // });
18+
// // const personalEmail = getSsmPersonalEmail(scope);
19+
// // topic.addSubscription(new EmailSubscription(personalEmail));
20+
// // return topic;
21+
// // }
22+
23+
// export function createPipe(scope: Construct, table: aws_dynamodb.Table): void {
24+
// //TODO: EventBridge Pipes L2 construct is not ready
25+
// const cdcPipeRole = new Role(scope, 'CDCPipeRole', {
26+
// assumedBy: new ServicePrincipal('pipes.amazonaws.com'),
27+
// });
28+
// table.grantStreamRead(cdcPipeRole);
29+
// // Creation of the Pipe
30+
// new CfnPipe(scope, 'CDCStreamPipe', {
31+
// name: 'CDCStreamPipe',
32+
// roleArn: cdcPipeRole.roleArn,
33+
// source: table.tableStreamArn!,
34+
// sourceParameters: {
35+
// dynamoDbStreamParameters: { batchSize: 10, startingPosition: 'LATEST' },
36+
// },
37+
// target: bus.eventBusArn,
38+
// targetParameters: {
39+
// eventBridgeEventBusParameters: {
40+
// source: PROJECT_SOURCE,
41+
// detailType: CDC_EVENT,
42+
// },
43+
// },
44+
// });
45+
// const bus = EventBus.fromEventBusName(this, 'DefaultBus', 'default');
46+
// bus.grantPutEventsTo(cdcPipeRole);
47+
// }

backend/events/lambda.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// import { unmarshall } from '@aws-sdk/util-dynamodb';
2+
3+
// import type { AttributeValue } from '@aws-sdk/client-dynamodb';
4+
// import type { DynamoDBRecord } from 'aws-lambda';
5+
// import { TABLE_PK, TABLE_SK } from '../constants';
6+
7+
// export const handler = async (records: DynamoDBRecord[]) => {
8+
// return records.map((r) => {
9+
// const oldImage = r.dynamodb?.OldImage
10+
// ? unmarshall(r.dynamodb?.OldImage as Record<string, AttributeValue>)
11+
// : undefined;
12+
// const newImage = r.dynamodb?.NewImage
13+
// ? unmarshall(r.dynamodb?.NewImage as Record<string, AttributeValue>)
14+
// : undefined;
15+
// const result: {
16+
// meta: Record<string, string>;
17+
// data: {
18+
// pk: string;
19+
// sk: string;
20+
// eventName: string;
21+
// eventType: string;
22+
// NewImage?: Record<string, unknown>;
23+
// OldImage?: Record<string, unknown>;
24+
// };
25+
// } = {
26+
// meta: {
27+
// fn: process.env.AWS_LAMBDA_FUNCTION_NAME!,
28+
// },
29+
// data: {
30+
// pk: oldImage?.[TABLE_PK] || newImage?.[TABLE_PK] || '',
31+
// sk: oldImage?.[TABLE_SK] || newImage?.[TABLE_SK] || '',
32+
// eventName: r.eventName || 'UNKNOWN',
33+
// eventType:
34+
// oldImage && newImage ? 'UPDATE' : oldImage ? 'REMOVE' : 'INSERT',
35+
// },
36+
// };
37+
// if (newImage) result.data.NewImage = newImage;
38+
// if (oldImage) result.data.OldImage = oldImage;
39+
// return result;
40+
// });
41+
// };

cicd/infrastructure/codepipeline.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export function createCodePipeline(
5959
}),
6060
description: 'Build phase for blog-backend-infrastructure resources',
6161
environment: {
62-
computeType: codebuild.ComputeType.SMALL,
63-
buildImage: codebuild.LinuxBuildImage.AMAZON_LINUX_2_4,
62+
computeType: codebuild.ComputeType.LAMBDA_1GB,
63+
buildImage: codebuild.LinuxArmLambdaBuildImage.AMAZON_LINUX_2023_NODE_22,
6464
},
6565
});
6666
buildProject.addToRolePolicy(
@@ -89,6 +89,7 @@ export function createCodePipeline(
8989
// Create the pipeline
9090
const pipeline = new codepipeline.Pipeline(scope, name + '-pipeline', {
9191
pipelineName: name,
92+
pipelineType: codepipeline.PipelineType.V2,
9293
stages: [
9394
{
9495
stageName: 'Source',

0 commit comments

Comments
 (0)