Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3ee0e2b

Browse files
committedAug 12, 2024
rollup config for typescript project
1 parent b69aa85 commit 3ee0e2b

13 files changed

+6914
-1
lines changed
 

‎.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out

‎.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out

‎README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# rollup-typescript-aws-serverless
1+
# Welcome to your CDK TypeScript project
2+
3+
This is a blank project for CDK development with TypeScript.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
## Useful commands
8+
9+
* `npm run build` compile typescript to js
10+
* `npm run watch` watch for changes and compile
11+
* `npm run test` perform the jest unit tests
12+
* `npx cdk deploy` deploy this stack to your default AWS account/region
13+
* `npx cdk diff` compare deployed stack with current state
14+
* `npx cdk synth` emits the synthesized CloudFormation template
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { RollupTypescriptAwsServerlessStack } from '../lib/rollup-typescript-aws-serverless-stack';
5+
6+
const app = new cdk.App();
7+
new RollupTypescriptAwsServerlessStack(app, 'RollupTypescriptAwsServerlessStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
env: { account: '857337530366', region: 'us-east-2' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});

‎cdk.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/rollup-typescript-aws-serverless.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
21+
"@aws-cdk/core:checkSecretUsage": true,
22+
"@aws-cdk/core:target-partitions": [
23+
"aws",
24+
"aws-cn"
25+
],
26+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
27+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
28+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
29+
"@aws-cdk/aws-iam:minimizePolicies": true,
30+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
31+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
32+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
33+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
34+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
35+
"@aws-cdk/core:enablePartitionLiterals": true,
36+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
37+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
38+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
39+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
40+
"@aws-cdk/aws-route53-patters:useCertificate": true,
41+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
42+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
43+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
44+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
45+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
46+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
47+
"@aws-cdk/aws-redshift:columnId": true,
48+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
49+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
50+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
51+
"@aws-cdk/aws-kms:aliasNameRef": true,
52+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
53+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
54+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
55+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
56+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
57+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
58+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
59+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
60+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
61+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
62+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
63+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
64+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
65+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
66+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
67+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
68+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
69+
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false,
70+
"@aws-cdk/aws-stepfunctions-tasks:ecsReduceRunTaskPermissions": true
71+
}
72+
}

‎jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
3+
import * as lambda from 'aws-cdk-lib/aws-lambda';
4+
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
5+
import * as sqs from 'aws-cdk-lib/aws-sqs';
6+
import * as lambdaEventSources from 'aws-cdk-lib/aws-lambda-event-sources';
7+
8+
export class RollupTypescriptAwsServerlessStack extends cdk.Stack {
9+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
10+
super(scope, id, props);
11+
12+
// Create a DynamoDB table
13+
const table = new dynamodb.Table(this, 'MyTable', {
14+
partitionKey: { name: 'messageId', type: dynamodb.AttributeType.STRING },
15+
removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
16+
});
17+
18+
// Create an SQS queue
19+
const queue = new sqs.Queue(this, 'MyQueue', {
20+
visibilityTimeout: cdk.Duration.seconds(30),
21+
});
22+
23+
// Create a Lambda function
24+
const myLambda = new lambda.Function(this, 'MyLambdaFunction', {
25+
runtime: lambda.Runtime.NODEJS_18_X,
26+
handler: 'lambdaExample.handler',
27+
code: lambda.Code.fromAsset('dist'),
28+
environment: {
29+
TABLE_NAME: table.tableName,
30+
},
31+
});
32+
33+
// Grant Lambda function permissions to interact with DynamoDB
34+
table.grantReadWriteData(myLambda);
35+
36+
// Add SQS as an event source for the Lambda function
37+
myLambda.addEventSource(new lambdaEventSources.SqsEventSource(queue));
38+
}
39+
}
40+

‎package-lock.json

Lines changed: 6607 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "rollup-typescript-aws-serverless",
3+
"version": "0.1.0",
4+
"bin": {
5+
"rollup-typescript-aws-serverless": "bin/rollup-typescript-aws-serverless.js"
6+
},
7+
"scripts": {
8+
"build": "rollup --config",
9+
"watch": "tsc -w",
10+
"test": "jest",
11+
"cdk": "cdk"
12+
},
13+
"devDependencies": {
14+
"@rollup/plugin-commonjs": "^26.0.1",
15+
"@rollup/plugin-json": "^6.1.0",
16+
"@rollup/plugin-node-resolve": "^15.2.3",
17+
"@rollup/plugin-typescript": "^11.1.6",
18+
"@types/aws-lambda": "^8.10.143",
19+
"@types/jest": "^29.5.12",
20+
"@types/node": "20.14.9",
21+
"aws-cdk": "2.150.0",
22+
"jest": "^29.7.0",
23+
"rollup": "^4.20.0",
24+
"ts-jest": "^29.1.5",
25+
"ts-node": "^10.9.2",
26+
"tslib": "^2.6.3",
27+
"typescript": "~5.5.3"
28+
},
29+
"dependencies": {
30+
"@aws-sdk/client-dynamodb": "^3.624.0",
31+
"@aws-sdk/lib-dynamodb": "^3.624.0",
32+
"aws-cdk-lib": "2.150.0",
33+
"aws-lambda": "^1.0.7",
34+
"constructs": "^10.0.0",
35+
"source-map-support": "^0.5.21"
36+
}
37+
}

‎rollup.config.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import nodeResolve from "@rollup/plugin-node-resolve";
2+
import json from "@rollup/plugin-json";
3+
import commonjs from "@rollup/plugin-commonjs";
4+
import typescript from "@rollup/plugin-typescript";
5+
6+
export default {
7+
input: 'src/lambdaExample.ts',
8+
output: {
9+
dir: './dist',
10+
format: 'cjs'
11+
},
12+
plugins: [
13+
nodeResolve({exportConditions: ["node"]}),
14+
commonjs(),
15+
json(),
16+
typescript({tsconfig: './tsconfig.json'})
17+
]
18+
};

‎src/lambdaExample.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { SQSEvent, SQSHandler } from 'aws-lambda';
2+
import { DynamoDB } from '@aws-sdk/client-dynamodb';
3+
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
4+
5+
const client = new DynamoDB({});
6+
const dynamoDb = DynamoDBDocument.from(client);
7+
const tableName = process.env.TABLE_NAME!;
8+
9+
export const handler: SQSHandler = async (event: SQSEvent) => {
10+
for (const record of event.Records) {
11+
const messageId = record.messageId;
12+
const body = record.body;
13+
14+
// Check if the message already exists in DynamoDB
15+
const existingItem = await dynamoDb.get({
16+
TableName: tableName,
17+
Key: { messageId },
18+
});
19+
20+
if (existingItem.Item) {
21+
console.log(`Message with ID ${messageId} already exists. Ignoring.`);
22+
continue;
23+
}
24+
25+
// Store the message in DynamoDB
26+
await dynamoDb.put({
27+
TableName: tableName,
28+
Item: { messageId, body },
29+
});
30+
31+
console.log(`Message with ID ${messageId} stored successfully.`);
32+
}
33+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// import * as cdk from 'aws-cdk-lib';
2+
// import { Template } from 'aws-cdk-lib/assertions';
3+
// import * as RollupTypescriptAwsServerless from '../lib/rollup-typescript-aws-serverless-stack';
4+
5+
// example test. To run these tests, uncomment this file along with the
6+
// example resource in lib/rollup-typescript-aws-serverless-stack.ts
7+
test('SQS Queue Created', () => {
8+
// const app = new cdk.App();
9+
// // WHEN
10+
// const stack = new RollupTypescriptAwsServerless.RollupTypescriptAwsServerlessStack(app, 'MyTestStack');
11+
// // THEN
12+
// const template = Template.fromStack(stack);
13+
14+
// template.hasResourceProperties('AWS::SQS::Queue', {
15+
// VisibilityTimeout: 300
16+
// });
17+
});

‎tsconfig.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
//"module": "esnext", use it when running typescript lambda code , otherwise this tsconfig for cdk is making sense
6+
//"moduleResolution": "node",
7+
"lib": [
8+
"es2020",
9+
"dom"
10+
],
11+
"declaration": false,
12+
"strict": true,
13+
"noImplicitAny": true,
14+
"strictNullChecks": true,
15+
"noImplicitThis": true,
16+
"alwaysStrict": true,
17+
"noUnusedLocals": false,
18+
"noUnusedParameters": false,
19+
"noImplicitReturns": true,
20+
"noFallthroughCasesInSwitch": false,
21+
"inlineSourceMap": true,
22+
"inlineSources": true,
23+
"experimentalDecorators": true,
24+
"strictPropertyInitialization": false,
25+
"typeRoots": [
26+
"./node_modules/@types"
27+
]
28+
},
29+
"exclude": [
30+
"node_modules",
31+
"cdk.out"
32+
]
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.