Amazon Lambda compute samples with Amazon CodeBuild
These groups of samples can be used to experiment with Amazon Lambda compute in CodeBuild.
Java sample
The Amazon Serverless Application Model (Amazon SAM) is an open-source framework for building
serverless applications. For more information, see the Amazon serverless
application model
Your buildspec may look similar to the following:
version: 0.2 env: variables: GRADLE_DIR: "GradleLambda/HelloWorldFunction" phases: build: commands: - echo "Running unit tests..." - ./$GRADLE_DIR/gradlew test -p $GRADLE_DIR - echo "Running build..." - sam build --template-file template.yaml - cd .aws-sam/build/ - sam package --output-template-file packaged.yaml --resolve-s3 --template-file template.yaml --region us-east-1 artifacts: name: "build-output" files: - "**/*" reports: test-report: base-directory: 'GradleLambda/HelloWorldFunction/build' files: - './test-results/**/*' file-format: 'JUNITXML' coverage-report: base-directory: 'GradleLambda/HelloWorldFunction/build' files: - './coverage-reports/**/*' file-format: 'JACOCOXML'
Your Amazon SAM template may look similar to the following:
Transform: AWS::Serverless-2016-10-31 Description: lambdas Globals: Function: Timeout: 20 Resources: GradleLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: GradleLambda/HelloWorldFunction Runtime: java17 Handler: helloworld.App::handleRequest Description: GradleLambda Events: GradleLambda: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api Properties: Path: /GradleLambda Method: get MemorySize: 512 Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object Variables: PARAM1: VALUE Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function # Find out more about other implicit resources you can reference within SAM # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api GradleLambdaApi: Description: "API Gateway endpoint URL for Prod stage for Hello World function" Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/GradleLambda/" GradleLambdaFunction: Description: "Hello World Lambda Function ARN" Value: !GetAtt GradleLambdaFunction.Arn GradleLambdaFunctionIamRole: Description: "Implicit IAM Role created for Hello World function" Value: !GetAtt GradleLambdaFunctionRole.Arn
For more information, see Use Amazon CodeBuild with serverless applications.
Node.js sample
Create React App is a way to create single-page React applications. You can use Create React App to build and test a web application and export the compiled bundle.
You can get started by doing the following:
yarn create-react-app my-app
You can use Node.js to build and test a React application. Your buildspec may look similar to the following:
version: 0.2 phases: build: commands: - npm install -g yarn - yarn - npm run build - npm run test -- --coverage --watchAll=false --testResultsProcessor="jest-junit" artifacts: name: "build-output" files: - "**/*" reports: test-report: files: - 'junit.xml' file-format: 'JUNITXML' coverage-report: files: - 'coverage/clover.xml' file-format: 'CLOVERXML'
For more information, see Create React App
.NET sample
The Serverless Framework is a framework which helps you use and deploy Lambda functions. The following .NET sample uses the Serverless Framework to build and test a Amazon Lambda function.
Your buildspec may look similar to the following:
version: 0.2 phases: build: commands: - echo "Running build..." - dotnet build DotNetServerlessApp.sln -c Release - echo "Running unit tests..." - dotnet test DotNetServerlessApp.sln -c Release --no-build --logger "trx" --results-directory ./test-results --collect:"XPlat Code Coverage" artifacts: name: "build-output" files: - "**/*" reports: test-report: files: - './test-results/**/*' file-format: 'VisualStudioTrx' coverage-report: files: - './test-results/**/*' file-format: 'CoberturaXML'
Your Serverless Framework template may look similar to the following:
{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Description": "An AWS Serverless Application.", "Resources": { "Get": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "DotNetServerlessApp::DotNetServerlessApp.Functions::Get", "Runtime": "dotnet6", "CodeUri": "", "MemorySize": 256, "Timeout": 30, "Role": null, "Policies": [ "AWSLambdaBasicExecutionRole" ], "Events": { "RootGet": { "Type": "Api", "Properties": { "Path": "/", "Method": "GET" } } } } } }, "Outputs": { "ApiURL": { "Description": "API endpoint URL for Prod environment", "Value": { "Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/" } } } }