Coder Social home page Coder Social logo

cdk-patterns / serverless Goto Github PK

View Code? Open in Web Editor NEW
2.2K 58.0 268.0 60.95 MB

This is intended to be a repo containing all of the official AWS Serverless architecture patterns built with CDK for developers to use. All patterns come in Typescript and Python with the exported CloudFormation also included.

Home Page: https://cdkpatterns.com

License: MIT License

JavaScript 8.64% TypeScript 34.22% HTML 6.42% CSS 0.23% Python 25.01% Batchfile 1.32% Dockerfile 0.12% Makefile 0.08% C# 3.38% Java 3.31% Shell 0.31% Jupyter Notebook 16.96%
serverless cdk aws lambda-functions dynamodb stepfunctions eventbridge sqs cloudformation typescript

serverless's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serverless's Issues

s3-react-website CREATE_FAILED The runtime parameter of python3.6 is no longer supported

Trying out the s3-react-website which should be a pretty common pattern. Doesn't look like it's been updated in over 2 years and probably why it's failing. This is the error I'm getting below:

4:36:48โ€ฏPM | CREATE_FAILED        | AWS::Lambda::Function       | CustomCDKBucketDep...CCXXXX
Resource handler returned message: "The runtime parameter of python3.6 is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (python3.9) while creating or updating functions. (Service: Lambda, Status Code: 400, Request ID: xxx)" (RequestToken: xxx, HandlerErrorCode: InvalidRequest)

New stack - Dynamodb atomic counter

Last month I had to implement a system on a client that uses many incremental keys for many different systems and architectures. The requirements were:
1 - Must be fast
2 - Must be serverless
3 - Must be resilient
4 - It must be cheap
5 - This should ensure that the numbers are not repeated, even with millions of requests.

So, I implemented a solution based on this article [1] and using the CDK.

I hope this stack helps other people

[1] https://itnext.io/simple-atomic-counter-with-dynamodb-and-api-gateway-e72115c209ff

Getting node10 error when trying to deploy this (Python version)

Im just learning CDK at the mo and have decided to start with something applicable to my job with the ETL pipeline. So apologies if this is not an issue with the code and is my lack of knowledge.

I cannot locate how to set the runtime to a newer version of node for this Lambda runtime of this function.

Getting an error:
20:32:38 | CREATE_FAILED | AWS::Lambda::Function | BucketNotification...094a3db8347ECC3691 Resource handler returned message: "The runtime parameter of nodejs10.x is no longer supported for creating or updating AWS Lambda functions

Looks like it related to the part of the of the template:

    Type: AWS::Lambda::Function
    Properties:
      Description: AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)
      Code:
        ZipFile: |-
          exports.handler = (event, context) => {
              // eslint-disable-next-line @typescript-eslint/no-require-imports, import/no-extraneous-dependencies
              const s3 = new (require('aws-sdk').S3)();
              // eslint-disable-next-line @typescript-eslint/no-require-imports
              const https = require('https');
              // eslint-disable-next-line @typescript-eslint/no-require-imports
              const url = require('url');
              log(JSON.stringify(event, undefined, 2));
              const props = event.ResourceProperties;
              if (event.RequestType === 'Delete') {
                  props.NotificationConfiguration = {}; // this is how you clean out notifications
              }
              const req = {
                  Bucket: props.BucketName,
                  NotificationConfiguration: props.NotificationConfiguration,
              };
              return s3.putBucketNotificationConfiguration(req, (err, data) => {
                  log({ err, data });
                  if (err) {
                      return submitResponse('FAILED', err.message + `\nMore information in CloudWatch Log Stream: ${context.logStreamName}`);
                  }
                  else {
                      return submitResponse('SUCCESS');
                  }
              });
              function log(obj) {
                  console.error(event.RequestId, event.StackId, event.LogicalResourceId, obj);
              }
              // eslint-disable-next-line max-len
              // adapted from https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#cfn-lambda-function-code-cfnresponsemodule
              // to allow sending an error messge as a reason.
              function submitResponse(responseStatus, reason) {
                  const responseBody = JSON.stringify({
                      Status: responseStatus,
                      Reason: reason || 'See the details in CloudWatch Log Stream: ' + context.logStreamName,
                      PhysicalResourceId: event.PhysicalResourceId || event.LogicalResourceId,
                      StackId: event.StackId,
                      RequestId: event.RequestId,
                      LogicalResourceId: event.LogicalResourceId,
                      NoEcho: false,
                  });
                  log({ responseBody });
                  const parsedUrl = url.parse(event.ResponseURL);
                  const options = {
                      hostname: parsedUrl.hostname,
                      port: 443,
                      path: parsedUrl.path,
                      method: 'PUT',
                      headers: {
                          'content-type': '',
                          'content-length': responseBody.length,
                      },
                  };
                  const request = https.request(options, (r) => {
                      log({ statusCode: r.statusCode, statusMessage: r.statusMessage });
                      context.done();
                  });
                  request.on('error', (error) => {
                      log({ sendError: error });
                      context.done();
                  });
                  request.write(responseBody);
                  request.end();
              }
          };
      Handler: index.handler
      Role:
        Fn::GetAtt:
          - BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC
          - Arn
      Runtime: nodejs10.x
      Timeout: 300
    DependsOn:
      - BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36
      - BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC
    Metadata:
      aws:cdk:path: the-eventbridge-etl/BucketNotificationsHandler050a0587b7544547bf325f094a3db834/Resource```

The RDS proxy stack failing to build

Hello,

Thanks for creating this amazing stacks .

Running the the-rds-proxy-stack typescript version is giving me this error.

DB Subnet Group doesn't meet availability zone coverage requirement. Please add subnets to cover at least 2 availabilit y zones. Current coverage: 1 (Service: AmazonRDS; Status Code: 400; Error Code: DBSubnetGroupDoesNotCoverEnoughAZs; Req uest ID: f6a214d1-f292-4dee-a1dc-c35e8589237c; Proxy: null)

How to set the max number of concurrent Lambda executions?

https://github.com/cdk-patterns/serverless/blob/main/the-scalable-webhook/README.md

Now we have our messages in a queue but we need to subscribe to the queue and insert the records into the DB. To do this we create a throttled lambda where we set the max number of concurrent executions to whatever scale we are happy with. This should be less than the max connections on our DB and should take into account any other Lambdas running in this account.

How does one achieve that?

Thanks

TheXrayDynamoFlow deploy error (typescript)

When I try to deploy TheXrayDynamoFlow as part of the TheXrayTracerStack, then I get the error:

TheXrayDynamoFlow failed: Error: This stack uses assets, so the toolkit stack must be deployed to the environment (Run "cdk bootstrap aws://unknown-account/unknown-region")

When I try to bootstrap (again), I get the error:

Environment aws://<account>/<region> failed bootstrapping: Error: Not downgrading existing bootstrap stack from version '4' to version '0'

Any ideas?

I tried with original version 1.60.0 and with 1.74.0.

the-big-fan npm run deploy result in error

Steps:
On cloud9
git clone https://github.com/cdk-patterns/serverless.git
cd the-big-hit
npm run build
npm run deploy

9:08:16 PM | CREATE_FAILED        | AWS::Lambda::Function           | SQSCreatedStatusSu...bdaHandler0467DB95
Resource handler returned message: "Uploaded file must be a non-empty zip (Service: Lambda, Status Code: 400, Request ID: b2f597b6-3556-4df6-8d90-3541ed3ed7c0)" (RequestToken: b2579
b25-26d2-662e-ff4b-28fb634df493, HandlerErrorCode: InvalidRequest)

        new Function (/home/ec2-user/environment/serverless/the-big-fan/typescript/node_modules/@aws-cdk/aws-lambda/lib/function.ts:608:35)
        \_ new TheBigFanStack (/home/ec2-user/environment/serverless/the-big-fan/typescript/lib/the-big-fan-stack.ts:65:45)
        \_ Object.<anonymous> (/home/ec2-user/environment/serverless/the-big-fan/typescript/bin/the-big-fan.ts:7:1)
        \_ Module._compile (node:internal/modules/cjs/loader:1126:14)
        \_ Module.m._compile (/home/ec2-user/environment/serverless/the-big-fan/typescript/node_modules/ts-node/src/index.ts:1056:23)
        \_ Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
        \_ Object.require.extensions.<computed> [as .ts] (/home/ec2-user/environment/serverless/the-big-fan/typescript/node_modules/ts-node/src/index.ts:1059:12)
        \_ Module.load (node:internal/modules/cjs/loader:1004:32)
        \_ Function.Module._load (node:internal/modules/cjs/loader:839:12)

Same issue with the-scalable-webhook

S3ReactWebsite failed

I'm starting completely fresh here so I could have missed something in my setup.

I followed these steps: https://cdkworkshop.com/15-prerequisites/200-account.html

  • using a personal AWS account
  • using VS Code.

When running the s3-react-website cdk pattern I hit the following error:

3ReactWebsite failed: Error: This stack uses assets, so the toolkit stack must be deployed to the environment (Run "cdk bootstrap aws://unknown-account/unknown-region")

I then ran:

cdk bootstrap aws://unknown-account/unknown-region

Which resolved the issue but trying understanding how or why it didn't pick my AWS user profile that also has the region defined.

Livemedia Stack

Hello, first of all, thank you very much for this incredible project.

I am building an infrastructure to deploy a Livemedia workflow for a customer. This workflow includes MediaPackage + MediaLive + S3 (a simple page with a player) and I'm writing using CDK + boto3, because MediaPackage is not available in CDK. Can I build this stack and open a PR here?

Thank you.

Private API Gateway

Looking to add a simple pattern demonstrating how to place an API Gateway and Lambda functions inside a VPC.

This pattern was requested in slack.

private-api-gateway

docker login error

[100%] fail: docker login --username AWS --password-stdin https://315997497220.dkr.ecr.us-west-2.amazonaws.com exited with error code 1:

โŒ the-eventbridge-etl failed: Error: Failed to publish one or more assets. See the error messages above for more information.
at Object.publishAssets (/home/mubashir/.nvm/versions/node/v16.3.0/lib/node_modules/aws-cdk/lib/util/asset-publishing.ts:25:11)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Object.deployStack (/home/mubashir/.nvm/versions/node/v16.3.0/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:237:3)
at CdkToolkit.deploy (/home/mubashir/.nvm/versions/node/v16.3.0/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:194:24)
at initCommandLine (/home/mubashir/.nvm/versions/node/v16.3.0/lib/node_modules/aws-cdk/bin/cdk.ts:267:9)
Failed to publish one or more assets. See the error messages above for more information.

I am getting this error when I deploy.

npx cdkp init the-eventbridge-etl --lang=python
cd the-eventbridge-etl
python3 -m venv .env
source .env/bin/activate
pip install -r requirements.txt
cdk synth
cdk deploy

The first error I get is related to bootstrapping. So I bootstrap.

export CDK_NEW_BOOTSTRAP=1
npx cdk bootstrap aws://315997497220/us-east-2 --cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess --trust 315997497220 aws://315997497220/us-east-2

But I get this error and I don't know what the issue is. I have not made any changes yet.

The Scheduled Lambda (Typescript)

I want to work on a simple pattern demonstrating how to set up a lambda function to run on a schedule using EventBridge rules that writes data to DynamoDB on each run.

The Simple Scheduled Lambda (1)

[Discussion] Next.js application pattern

Hello and greetings from Sonos!

First off, I just wanted to say thank you for supplying an amazing repo of patterns. This has inspired us to start tackling one of our more challenging deployments and hopefully convert it to something anyone can use.

I wanted to chat about application frameworks like Next.JS. These frameworks provide end-to-end application development experience that makes it very appealing to development teams. The only issue is that they want you to spend money on their hosting platform versus providing any guidance on how to host yourself.

https://nextjs.org/docs/deployment

Their basic solution for you is to spin up an EC2 and run the start script. I'm sure we could also explore using ECR/ECS with containers but where's the fun in that. We want a serverless solution!

The Serverless community has built a somewhat working solution here https://serverless-nextjs.com/ but its definitely lacking Next.JS features and I'm not ๐Ÿ’ฏ sure about their approach. CloudFront + Lambda@edge used for APIs, etc.

The Terraform community has built another solution here https://registry.terraform.io/modules/dealmore/next-js/aws/latest and is also lacking some core NextJS features and appears to still be in active development. I do like their approach to the serverless architecture here. It just feels better and isn't forced like the Serverless approach is.

I was hoping this community could band together and vet each solution to come up with a proper pattern for these types of application frameworks ๐Ÿคž

Cheers,

Andrew

rds proxy code doesn't work with mysql8

you have to update cdk in order to specify the mysql version

    const rdsInstance = new rds.DatabaseInstance(this, 'DBInstance', {
      engine: rds.DatabaseInstanceEngine.mysql({
		  version: rds.MysqlEngineVersion.VER_5_7_30
	  }),

also the secret has changed to secrets andis now an array:

    // Create an RDS Proxy
    const proxy = rdsInstance.addProxy(id+'-proxy', {
        secrets: [databaseCredentialsSecret],
        debugLogging: true,
        vpc,
        securityGroups: [dbConnectionGroup]
    });

also you need to specify the region in the bin/the-rds-proxy.ts

new TheRdsProxyStack(app, 'TheRdsProxyStack',{
    env: {region: "us-east-1"}
});

AWS IoT with a static endpoint

I am looking for a way to manage MQTT brokers with ease (that is why I created the-basic-mq), and I found this re:Invent presentation:

AWS re:Invent 2019: [REPEAT 1] Simplify the migration of IoT applications to AWS IoT (IOT341-R1)

PDF

Though this is only for us-east-1 region, the architecture could be better than the one with AmazonMQ, if you choose MQTT as a message queueing protocol (you can use lambda as an authorizer).

I can try out this solution with CDK, once I finish with the-basic-mq Python implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.