diff --git a/16-SAM/Practice-16.1/sam-app/.gitignore b/16-SAM/Practice-16.1/sam-app/.gitignore new file mode 100644 index 00000000..4bccb52c --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/.gitignore @@ -0,0 +1,345 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam +# Edit at https://www.toptal.com/developers/gitignore?templates=osx,linux,python,windows,pycharm,visualstudiocode,sam + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +# .env +.env/ +.venv/ +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# operating system-related files +# file properties cache/storage on macOS +*.DS_Store +# thumbnail cache on Windows +Thumbs.db + +# profiling data +.prof + + +### SAM ### +# Ignore build directories for the AWS Serverless Application Model (SAM) +# Info: https://aws.amazon.com/serverless/sam/ +# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html + +**/.aws-sam + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam diff --git a/16-SAM/Practice-16.1/sam-app/README.md b/16-SAM/Practice-16.1/sam-app/README.md new file mode 100644 index 00000000..c895f9c8 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/README.md @@ -0,0 +1,126 @@ +# sam-app + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-app$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-app$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-app$ sam local start-api +sam-app$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-app$ sam logs -n HelloWorldFunction --stack-name sam-app --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the [pytest](https://docs.pytest.org/en/latest/) and run unit tests. + +```bash +sam-app$ pip install pytest pytest-mock --user +sam-app$ python -m pytest tests/ -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-app +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.1/sam-app/__init__.py b/16-SAM/Practice-16.1/sam-app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/env.json b/16-SAM/Practice-16.1/sam-app/env.json new file mode 100644 index 00000000..2cc92943 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/env.json @@ -0,0 +1,5 @@ +{ + "HelloWorldFunction": { + "NAME": "desmond.ndambi.labs" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/events/custom.json b/16-SAM/Practice-16.1/sam-app/events/custom.json new file mode 100644 index 00000000..e379b8cd --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/events/custom.json @@ -0,0 +1,123 @@ +{ + "body": "eyJ0ZXN0IjoiYm9keSJ9", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": true, + "queryStringParameters": { + "foo": "bar" + }, + "multiValueQueryStringParameters": { + "foo": [ + "bar" + ] + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "multiValueHeaders": { + "Accept": [ + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" + ], + "Accept-Encoding": [ + "gzip, deflate, sdch" + ], + "Accept-Language": [ + "en-US,en;q=0.8" + ], + "Cache-Control": [ + "max-age=0" + ], + "CloudFront-Forwarded-Proto": [ + "https" + ], + "CloudFront-Is-Desktop-Viewer": [ + "true" + ], + "CloudFront-Is-Mobile-Viewer": [ + "false" + ], + "CloudFront-Is-SmartTV-Viewer": [ + "false" + ], + "CloudFront-Is-Tablet-Viewer": [ + "false" + ], + "CloudFront-Viewer-Country": [ + "US" + ], + "Host": [ + "0123456789.execute-api.us-east-1.amazonaws.com" + ], + "Upgrade-Insecure-Requests": [ + "1" + ], + "User-Agent": [ + "Custom User Agent String" + ], + "Via": [ + "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)" + ], + "X-Amz-Cf-Id": [ + "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==" + ], + "X-Forwarded-For": [ + "127.0.0.1, 127.0.0.2" + ], + "X-Forwarded-Port": [ + "443" + ], + "X-Forwarded-Proto": [ + "https" + ] + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/events/event.json b/16-SAM/Practice-16.1/sam-app/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/__init__.py b/16-SAM/Practice-16.1/sam-app/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/app.py b/16-SAM/Practice-16.1/sam-app/hello_world/app.py new file mode 100644 index 00000000..e283b1a8 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/hello_world/app.py @@ -0,0 +1,43 @@ +import json +import os + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + return { + "statusCode": 200, + "body": json.dumps({ + "message": "hello world", + "NAME": os.environ.get('NAME') + # "location": ip.text.replace("\n", "") + }), + } diff --git a/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt b/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.1/sam-app/packaged.yaml b/16-SAM/Practice-16.1/sam-app/packaged.yaml new file mode 100644 index 00000000..c0290bdc --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/packaged.yaml @@ -0,0 +1,42 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-app + + Sample SAM Template for sam-app + + ' +Globals: + Function: + Timeout: 3 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/1007ba6f2fd6aed6daf87b6f392f298d + Handler: app.lambda_handler + Runtime: python3.7 + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get + Metadata: + SamResourceId: HelloWorldFunction +Outputs: + HelloWorldApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/ + HelloWorldFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - HelloWorldFunction + - Arn + HelloWorldFunctionIamRole: + Description: Implicit IAM Role created for Hello World function + Value: + Fn::GetAtt: + - HelloWorldFunctionRole + - Arn diff --git a/16-SAM/Practice-16.1/sam-app/template.yaml b/16-SAM/Practice-16.1/sam-app/template.yaml new file mode 100644 index 00000000..a7e04a4b --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/template.yaml @@ -0,0 +1,45 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Parameters: + Name: + Type: String +Description: > + sam-app + + Sample SAM Template for sam-app + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.7 + Environment: + Variables: + NAME: !Ref Name + Events: + HelloWorld: + 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: /hello + Method: get + +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 + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/Practice-16.1/sam-app/tests/__init__.py b/16-SAM/Practice-16.1/sam-app/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/tests/unit/__init__.py b/16-SAM/Practice-16.1/sam-app/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py b/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.1/sam-app/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.2/sam-api/.gitignore b/16-SAM/Practice-16.2/sam-api/.gitignore new file mode 100644 index 00000000..4bccb52c --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/.gitignore @@ -0,0 +1,345 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam +# Edit at https://www.toptal.com/developers/gitignore?templates=osx,linux,python,windows,pycharm,visualstudiocode,sam + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +pytestdebug.log + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +# .env +.env/ +.venv/ +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# operating system-related files +# file properties cache/storage on macOS +*.DS_Store +# thumbnail cache on Windows +Thumbs.db + +# profiling data +.prof + + +### SAM ### +# Ignore build directories for the AWS Serverless Application Model (SAM) +# Info: https://aws.amazon.com/serverless/sam/ +# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html + +**/.aws-sam + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/osx,linux,python,windows,pycharm,visualstudiocode,sam diff --git a/16-SAM/Practice-16.2/sam-api/README.md b/16-SAM/Practice-16.2/sam-api/README.md new file mode 100644 index 00000000..18ec17d7 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/README.md @@ -0,0 +1,126 @@ +# sam-api + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-api$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-api$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-api$ sam local start-api +sam-api$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-api$ sam logs -n HelloWorldFunction --stack-name sam-api --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the [pytest](https://docs.pytest.org/en/latest/) and run unit tests. + +```bash +sam-api$ pip install pytest pytest-mock --user +sam-api$ python -m pytest tests/ -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-api +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.2/sam-api/__init__.py b/16-SAM/Practice-16.2/sam-api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/events/event.json b/16-SAM/Practice-16.2/sam-api/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/__init__.py b/16-SAM/Practice-16.2/sam-api/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/app.py b/16-SAM/Practice-16.2/sam-api/hello_world/app.py new file mode 100644 index 00000000..732b1352 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/hello_world/app.py @@ -0,0 +1,39 @@ +import json + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + + return { + "statusCode": 200, + "body": event['pathStringParameters'], + } diff --git a/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt b/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-api/template.yaml b/16-SAM/Practice-16.2/sam-api/template.yaml new file mode 100644 index 00000000..70c9f0dc --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/template.yaml @@ -0,0 +1,39 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-api + + Sample SAM Template for sam-api + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.7 + Events: + HelloWorld: + 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: /hello + Method: get + +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 + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/Practice-16.2/sam-api/tests/__init__.py b/16-SAM/Practice-16.2/sam-api/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/tests/unit/__init__.py b/16-SAM/Practice-16.2/sam-api/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py b/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-api/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.2/sam-s3/__init__.py b/16-SAM/Practice-16.2/sam-s3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/events/event.json b/16-SAM/Practice-16.2/sam-s3/events/event.json new file mode 100644 index 00000000..84f760b6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/events/event.json @@ -0,0 +1,131 @@ +{ + "Records": [ + { + "eventVersion": "2.0", + "eventSource": "aws:s3", + "awsRegion": "us-east-1", + "eventTime": "1970-01-01T00:00:00.000Z", + "eventName": "ObjectCreated:Put", + "userIdentity": { + "principalId": "EXAMPLE" + }, + "requestParameters": { + "sourceIPAddress": "127.0.0.1" + }, + "responseElements": { + "x-amz-request-id": "EXAMPLE123456789", + "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH" + }, + "s3": { + "s3SchemaVersion": "1.0", + "configurationId": "testConfigRule", + "bucket": { + "name": "desmond-sam-bucket", + "ownerIdentity": { + "principalId": "EXAMPLE" + }, + "arn": "arn:aws:s3:::desmond-sam-bucket" + }, + "object": { + "key": "test/key", + "size": 1024, + "eTag": "0123456789abcdef0123456789abcdef", + "sequencer": "0A1B2C3D4E5F678901" + } + } + } + ] +} +{ + "Records": [ + { + "eventID": "c4ca4238a0b923820dcc509a6f75849b", + "eventName": "INSERT", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439091", + "SizeBytes": 26, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "c81e728d9d4c2f636f067f89cc14862c", + "eventName": "MODIFY", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439092", + "SizeBytes": 59, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "eccbc87e4b5ce2fe28308fd9f2a7baf3", + "eventName": "REMOVE", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439093", + "SizeBytes": 38, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-s3/events/sns.json b/16-SAM/Practice-16.2/sam-s3/events/sns.json new file mode 100644 index 00000000..5e9bd5cd --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/events/sns.json @@ -0,0 +1,31 @@ +{ + "Records": [ + { + "EventSource": "aws:sns", + "EventVersion": "1.0", + "EventSubscriptionArn": "arn:aws:sns:us-east-1::SampleTopic", + "Sns": { + "Type": "Notification", + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", + "TopicArn": "arn:aws:sns:us-east-1:123456789012:SampleTopic", + "Subject": "example subject", + "Message": "This lab is crazy", + "Timestamp": "1970-01-01T00:00:00.000Z", + "SignatureVersion": "1", + "Signature": "EXAMPLE", + "SigningCertUrl": "EXAMPLE", + "UnsubscribeUrl": "EXAMPLE", + "MessageAttributes": { + "Test": { + "Type": "String", + "Value": "TestString" + }, + "TestBinary": { + "Type": "Binary", + "Value": "TestBinary" + } + } + } + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-s3/packaged.yaml b/16-SAM/Practice-16.2/sam-s3/packaged.yaml new file mode 100644 index 00000000..7d6086e8 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/packaged.yaml @@ -0,0 +1,108 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-s3 + + Sample SAM Template for s3 + + ' +Globals: + Function: + Timeout: 3 +Resources: + s3Bucket: + Type: AWS::S3::Bucket + Properties: + BucketName: desmond-sam-bucket + Metadata: + SamResourceId: s3Bucket + SNSTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: SampleTopic + Metadata: + SamResourceId: SNSTopic + s3Function: + Type: AWS::Serverless::Function + DependsOn: + - SNSTopic + Properties: + CodeUri: s3://sam-bucket-desmond/f4a62871816679d0980b132eadcd3a82 + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - s3:GetObject + Resource: '*' + Events: + S3Event: + Type: S3 + Properties: + Bucket: + Ref: s3Bucket + Events: s3:ObjectCreated:* + SNSEvent: + Type: SNS + Properties: + Topic: + Ref: SNSTopic + Metadata: + SamResourceId: s3Function + Subscription: + Type: AWS::SNS::Subscription + DependsOn: + - SNSTopic + Properties: + Endpoint: desmond.ndambi@stelligent.com + Protocol: email + TopicArn: + Ref: SNSTopic + Metadata: + SamResourceId: Subscription + s3SubscripFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/b0ba279429f18268bc73cadf7319512e + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - sns:CreateTopic + - sns:Publish + - sns:Subscribe + - sns:CreateTopic + - sns:GetTopicAttributes + - sns:SetTopicAttributes + - sns:TagResource + - sns:UntagResource + - sns:ListTagsForResource + - sns:ListSubscriptionsByTopic + - s3:GetObject + - s3:ListBucket + Resource: '*' + Events: + SNSEvent: + Type: SNS + Properties: + Topic: + Ref: SNSTopic + Metadata: + SamResourceId: s3SubscripFunction +Outputs: + s3Function: + Description: Process S3 Function ARN + Value: + Fn::GetAtt: + - s3Function + - Arn + s3SubscripFunction: + Description: Process S3 Subscription Function ARN + Value: + Fn::GetAtt: + - s3SubscripFunction + - Arn diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3-sub/__init__.py b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py new file mode 100644 index 00000000..901869ce --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/process-s3-sub/app.py @@ -0,0 +1,39 @@ +import boto3 + +print('Loading function') + +sns = boto3.client('sns') +s3 = boto3.resource('s3') + + +def lambda_handler(event, context): + """ + Subscribes an endpoint to the topic. Some endpoint types, such as email, + must be confirmed before their subscriptions are active. When a subscription + is not confirmed, its Amazon Resource Number (ARN) is set to + 'PendingConfirmation'. + + :param topic: The topic to subscribe to. + :param protocol: The protocol of the endpoint, such as 'sms' or 'email'. + :param endpoint: The endpoint that receives messages, such as a phone number + or an email address. + :return: The newly added subscription. + """ + topic = sns.create_topic(Name='SampleTopic') + topic_arn = topic["TopicArn"] + # Create email subscription + response = sns.subscribe(TopicArn=topic_arn, Protocol="email", Endpoint="desmond.ndambi@stelligent.com") + subscription_arn = response["SubscriptionArn"] + + s3_bucket_name='desmond-sam-bucket' + + my_bucket=s3.Bucket(s3_bucket_name) + bucket_list = [] + for file in my_bucket.objects.all(): + # Un comment this to specify a files with particular extensions e.g .json. .csv + # file_name=file.key + # if file_name.find(".json")!=-1: + bucket_list.append(file.key) + length_bucket_list=print(len(bucket_list)) + print(bucket_list[0:10]) + diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3/__init__.py b/16-SAM/Practice-16.2/sam-s3/process-s3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-s3/process-s3/app.py b/16-SAM/Practice-16.2/sam-s3/process-s3/app.py new file mode 100644 index 00000000..6def667f --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/process-s3/app.py @@ -0,0 +1,42 @@ +from __future__ import print_function +import json +import urllib.parse +import boto3 + +print('Loading function') + +s3 = boto3.client('s3') +sns = boto3.resource('sns') + + +def lambda_handler(event, context): + #print("Received event: " + json.dumps(event, indent=2)) + + # Get the object from the event and show its content type + bucket = event['Records'][0]['s3']['bucket']['name'] + key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') + try: + response = s3.get_object(Bucket=bucket, Key=key) + print("CONTENT TYPE: " + response['ContentType']) + return response['ContentType'] + except Exception as e: + print(e) + print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) + raise e + + #print("Received event: " + json.dumps(event, indent=2)) + message = event['Records'][0]['Sns']['Message'] + print("From SNS: " + message) + + # Lambda function publish message to an SNS Topic + topic = sns.create_topic(Name='SampleTopic') + topic_arn = topic.arn + response = sns.publish ( + TargetArn = topic_arn, + Message = json.dumps({'default': message}), + MessageStructure = 'json' + ) + return { + 'statusCode': 200, + 'body': json.dumps(response) + } diff --git a/16-SAM/Practice-16.2/sam-s3/template.yml b/16-SAM/Practice-16.2/sam-s3/template.yml new file mode 100644 index 00000000..370e38ab --- /dev/null +++ b/16-SAM/Practice-16.2/sam-s3/template.yml @@ -0,0 +1,91 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-s3 + + Sample SAM Template for s3 + +Globals: + Function: + Timeout: 3 + +Resources: + s3Bucket: + Type: AWS::S3::Bucket + Properties: + BucketName: "desmond-sam-bucket" + SNSTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: "SampleTopic" + s3Function: + Type: AWS::Serverless::Function + DependsOn: + - SNSTopic + Properties: + CodeUri: process-s3/ + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - s3:GetObject + Resource: '*' + Events: + S3Event: + Type: S3 + Properties: + Bucket: !Ref s3Bucket + Events: s3:ObjectCreated:* + SNSEvent: + Type: SNS + Properties: + Topic: !Ref SNSTopic + Subscription: + Type: AWS::SNS::Subscription + DependsOn: + - SNSTopic + Properties: + Endpoint: desmond.ndambi@stelligent.com + Protocol: email + TopicArn: !Ref 'SNSTopic' + s3SubscripFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: process-s3-sub/ + Handler: app.lambda_handler + Runtime: python3.7 + Policies: + - Statement: + - Sid: VisualEditor0 + Effect: Allow + Action: + - sns:CreateTopic + - sns:Publish + - sns:Subscribe + - sns:CreateTopic + - sns:GetTopicAttributes + - sns:SetTopicAttributes + - sns:TagResource + - sns:UntagResource + - sns:ListTagsForResource + - sns:ListSubscriptionsByTopic + - s3:GetObject + - s3:ListBucket + Resource: "*" + Events: + SNSEvent: + Type: SNS + Properties: + Topic: !Ref SNSTopic + + +Outputs: + s3Function: + Description: "Process S3 Function ARN" + Value: !GetAtt s3Function.Arn + s3SubscripFunction: + Description: "Process S3 Subscription Function ARN" + Value: !GetAtt s3SubscripFunction.Arn diff --git a/16-SAM/Practice-16.2/sam-sqs/__init__.py b/16-SAM/Practice-16.2/sam-sqs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-sqs/config.sh b/16-SAM/Practice-16.2/sam-sqs/config.sh new file mode 100644 index 00000000..735e2e85 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/config.sh @@ -0,0 +1,2 @@ +#To invoke the lambda function with the generate event +aws lambda invoke --function-name SAM-SQS-ProcessSqsFunction-9VpBgMnNIswj --cli-binary-format raw-in-base64-out --payload file://events/event.json response.json --profile labs diff --git a/16-SAM/Practice-16.2/sam-sqs/events/event.json b/16-SAM/Practice-16.2/sam-sqs/events/event.json new file mode 100644 index 00000000..f697abde --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/events/event.json @@ -0,0 +1,20 @@ +{ + "Records": [ + { + "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78", + "receiptHandle": "MessageReceiptHandle", + "body": "Hello from SQS!", + "attributes": { + "ApproximateReceiveCount": "1", + "SentTimestamp": "1523232000000", + "SenderId": "123456789012", + "ApproximateFirstReceiveTimestamp": "1523232000001" + }, + "messageAttributes": {}, + "md5OfBody": "7b270e59b47ff90a553787216d55d91d", + "eventSource": "aws:sqs", + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:MyQueue", + "awsRegion": "us-east-1" + } + ] +} diff --git a/16-SAM/Practice-16.2/sam-sqs/packaged.yaml b/16-SAM/Practice-16.2/sam-sqs/packaged.yaml new file mode 100644 index 00000000..a9151c16 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/packaged.yaml @@ -0,0 +1,44 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-api + + Sample SAM Template for sam-sqs + + ' +Globals: + Function: + Timeout: 3 +Resources: + MessageQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: SampleQueue + Metadata: + SamResourceId: MessageQueue + ProcessSqsFunction: + Type: AWS::Serverless::Function + DependsOn: + - MessageQueue + Properties: + CodeUri: s3://sam-bucket-desmond/3154cf92b3f3b7dcb3b4cdfe0d1b6958 + Handler: app.lambda_handler + Runtime: python3.7 + Events: + SQSQueue: + Type: SQS + Properties: + Queue: + Fn::GetAtt: + - MessageQueue + - Arn + BatchSize: 10 + Enabled: false + Metadata: + SamResourceId: ProcessSqsFunction +Outputs: + ProcessSqsFunction: + Description: Process Queue Function ARN + Value: + Fn::GetAtt: + - ProcessSqsFunction + - Arn diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/__init__.py b/16-SAM/Practice-16.2/sam-sqs/process_sqs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py b/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py new file mode 100644 index 00000000..4a5da424 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/process_sqs/app.py @@ -0,0 +1,8 @@ +import json + +# import requests + +def lambda_handler(event, context): + for record in event['Records']: + payload=record["body"] + print(str(payload)) diff --git a/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt b/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/process_sqs/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-sqs/response.json b/16-SAM/Practice-16.2/sam-sqs/response.json new file mode 100644 index 00000000..ec747fa4 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/response.json @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/16-SAM/Practice-16.2/sam-sqs/template.yaml b/16-SAM/Practice-16.2/sam-sqs/template.yaml new file mode 100644 index 00000000..14871ed4 --- /dev/null +++ b/16-SAM/Practice-16.2/sam-sqs/template.yaml @@ -0,0 +1,36 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-sqs + + Sample SAM Template for sam-sqs + +Globals: + Function: + Timeout: 3 + +Resources: + MessageQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: "SampleQueue" + ProcessSqsFunction: + Type: AWS::Serverless::Function + DependsOn: + - MessageQueue + Properties: + CodeUri: process_sqs/ + Handler: app.lambda_handler + Runtime: python3.7 + Events: + SQSQueue: + Type: SQS + Properties: + Queue: !GetAtt MessageQueue.Arn + BatchSize: 10 + Enabled: false + +Outputs: + ProcessSqsFunction: + Description: "Process Queue Function ARN" + Value: !GetAtt ProcessSqsFunction.Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore b/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore new file mode 100644 index 00000000..4808264d --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/.gitignore @@ -0,0 +1,244 @@ + +# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Ruby plugin and RubyMine +/.rakeTasks + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +.pytest_cache/ +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +.history + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Build folder + +*/build/* + +# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode \ No newline at end of file diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/README.md b/16-SAM/Practice-16.3/sam-dynamoDb/README.md new file mode 100644 index 00000000..68134eb2 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/README.md @@ -0,0 +1,130 @@ +# sam-dynamoDb + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello_world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* [Python 3 installed](https://www.python.org/downloads/) +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build --use-container +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build --use-container` command. + +```bash +sam-dynamoDb$ sam build --use-container +``` + +The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-dynamoDb$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-dynamoDb$ sam local start-api +sam-dynamoDb$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-dynamoDb$ sam logs -n HelloWorldFunction --stack-name sam-dynamoDb --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Tests + +Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests. + +```bash +sam-dynamoDb$ pip install -r tests/requirements.txt --user +# unit test +sam-dynamoDb$ python -m pytest tests/unit -v +# integration test, requiring deploying the stack first. +# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing +sam-dynamoDb$ AWS_SAM_STACK_NAME= python -m pytest tests/integration -v +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-dynamoDb +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json b/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json new file mode 100644 index 00000000..b25872c6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/create-table.json @@ -0,0 +1,13 @@ +{ + "TableName": "DynamoDBTable", + "KeySchema": [ + { "AttributeName": "id", "KeyType": "HASH" } + ], + "AttributeDefinitions": [ + { "AttributeName": "id", "AttributeType": "S" } + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + } +} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml b/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml new file mode 100644 index 00000000..417a6875 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.5' + +services: + dynamodb-local: + container_name: dynamodb-local + image: "amazon/dynamodb-local:latest" + ports: + - "8000:8000" + working_dir: /home/dynamodblocal + command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data" + volumes: + - "./docker/dynamodb:/home/dynamodblocal/data" + networks: + - api + +networks: + api: diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db b/16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db new file mode 100644 index 00000000..068c7f4a Binary files /dev/null and b/16-SAM/Practice-16.3/sam-dynamoDb/docker/dynamodb/shared-local-instance.db differ diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json b/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json new file mode 100644 index 00000000..4458c684 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/events/event.json @@ -0,0 +1,93 @@ +{ + "Records": [ + { + "eventID": "c4ca4238a0b923820dcc509a6f75849b", + "eventName": "INSERT", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439091", + "SizeBytes": 26, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "c81e728d9d4c2f636f067f89cc14862c", + "eventName": "MODIFY", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "NewImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "New item!" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439092", + "SizeBytes": 59, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + }, + { + "eventID": "eccbc87e4b5ce2fe28308fd9f2a7baf3", + "eventName": "REMOVE", + "eventVersion": "1.1", + "eventSource": "aws:dynamodb", + "awsRegion": "us-east-1", + "dynamodb": { + "Keys": { + "Id": { + "N": "101" + } + }, + "OldImage": { + "Message": { + "S": "This item has changed" + }, + "Id": { + "N": "101" + } + }, + "ApproximateCreationDateTime": 1428537600, + "SequenceNumber": "4421584500000000017450439093", + "SizeBytes": 38, + "StreamViewType": "NEW_AND_OLD_IMAGES" + }, + "eventSourceARN": "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/stream/2015-06-27T00:48:05.899" + } + ] +} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py new file mode 100644 index 00000000..cf5e916c --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/app.py @@ -0,0 +1,56 @@ +import json +import os +import boto3 +import uuid + +# import requests + + +def lambda_handler(event, context): + """Sample pure Lambda function + + Parameters + ---------- + event: dict, required + API Gateway Lambda Proxy Input Format + + Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + + context: object, required + Lambda Context runtime methods and attributes + + Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html + + Returns + ------ + API Gateway Lambda Proxy Output Format: dict + + Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + """ + + # try: + # ip = requests.get("http://checkip.amazonaws.com/") + # except requests.RequestException as e: + # # Send some context about this error to Lambda Logs + # print(e) + + # raise e + # Create records + table_name = os.getenv('TABLE_NAME') + if os.getenv('AWS_SAM_LOCAL'): + table = boto3.resource('dynamodb', endpoint_url="http://0.0.0.0:8000/").Table(table_name) + # Inserting an item in a table + table.put_item(Item={'id': str(uuid.uuid4())}) + else: + region = os.getenv('AWS_REGION') + table = boto3.resource('dynamodb', region_name=region).Table(table_name) + + + return { + "statusCode": 200, + "body": json.dumps({ + "message": "hello world", + "TABLE_NAME": os.environ.get('TABLE_NAME'), + # "location": ip.text.replace("\n", "") + }), + } diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/hello_world/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml b/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml new file mode 100644 index 00000000..279da6e6 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/packaged.yaml @@ -0,0 +1,59 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-dynamoDb + + Sample SAM Template for sam-dynamoDb + + ' +Globals: + Function: + Timeout: 3 +Resources: + DynamoDBTable: + Type: AWS::DynamoDB::Table + Properties: + TableName: my-table + AttributeDefinitions: + - AttributeName: id + AttributeType: S + KeySchema: + - AttributeName: id + KeyType: HASH + ProvisionedThroughput: + ReadCapacityUnits: 5 + WriteCapacityUnits: 5 + Metadata: + SamResourceId: DynamoDBTable + WriteFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: s3://sam-bucket-desmond/3716a846e5024c1f2d3b61aae5d6822a + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Events: + HelloWorld: + Type: Api + Properties: + Path: /write + Method: get + Environment: + Variables: + TABLE_NAME: + Ref: DynamoDBTable + Metadata: + SamResourceId: WriteFunction +Outputs: + DynamoApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/write/ + WriteFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - WriteFunction + - Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py new file mode 100644 index 00000000..2e65e21b --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/streamdb/app.py @@ -0,0 +1,4 @@ +def lambda_handler(event, context): + for record in event['Records']: + print(record['eventID']) + print(record['eventName']) diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml b/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml new file mode 100644 index 00000000..2a5d0411 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/template.yaml @@ -0,0 +1,80 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-dynamoDb + + Sample SAM Template for sam-dynamoDb + +Globals: + Function: + Timeout: 3 + +Resources: + DynamoDBTable: + Type: AWS::DynamoDB::Table + Properties: + TableName: my-table + AttributeDefinitions: + - AttributeName: id + AttributeType: S + KeySchema: + - AttributeName: id + KeyType: HASH + ProvisionedThroughput: + ReadCapacityUnits: 5 + WriteCapacityUnits: 5 + StreamSpecification: + StreamViewType: NEW_IMAGE + WriteFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: hello_world/ + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Policies: + - DynamoDBCrudPolicy: + TableName: !Ref DynamoDBTable + Events: + HelloWorld: + Type: Api + Properties: + Path: /write + Method: get + Environment: + Variables: + TABLE_NAME: !Ref DynamoDBTable + ProcessStreamEventsFunction: + Type: AWS::Serverless::Function + DependsOn: + - DynamoDBTable + Properties: + CodeUri: streamdb/ + Handler: app.lambda_handler + Runtime: python3.9 + Architectures: + - x86_64 + Policies: + - DynamoDBStreamReadPolicy: + TableName: !Ref DynamoDBTable + StreamName: !GetAtt DynamoDBTable.StreamArn + Events: + DBStreams: + Type: DynamoDB + Properties: + Stream: !GetAtt DynamoDBTable.StreamArn + StartingPosition: TRIM_HORIZON + BatchSize: 10 + MaximumBatchingWindowInSeconds: 10 + Enabled: false + +Outputs: + DynamoApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/write/" + WriteFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt WriteFunction.Arn diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py new file mode 100644 index 00000000..b96e8033 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/integration/test_api_gateway.py @@ -0,0 +1,45 @@ +import os + +import boto3 +import pytest +import requests + +""" +Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test. +""" + + +class TestApiGateway: + + @pytest.fixture() + def api_gateway_url(self): + """ Get the API Gateway URL from Cloudformation Stack outputs """ + stack_name = os.environ.get("AWS_SAM_STACK_NAME") + + if stack_name is None: + raise ValueError('Please set the AWS_SAM_STACK_NAME environment variable to the name of your stack') + + client = boto3.client("cloudformation") + + try: + response = client.describe_stacks(StackName=stack_name) + except Exception as e: + raise Exception( + f"Cannot find stack {stack_name} \n" f'Please make sure a stack with the name "{stack_name}" exists' + ) from e + + stacks = response["Stacks"] + stack_outputs = stacks[0]["Outputs"] + api_outputs = [output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"] + + if not api_outputs: + raise KeyError(f"HelloWorldAPI not found in stack {stack_name}") + + return api_outputs[0]["OutputValue"] # Extract url from stack outputs + + def test_api_gateway(self, api_gateway_url): + """ Call the API Gateway endpoint and check the response """ + response = requests.get(api_gateway_url) + + assert response.status_code == 200 + assert response.json() == {"message": "hello world"} diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt b/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt new file mode 100644 index 00000000..b9cf27ab --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/requirements.txt @@ -0,0 +1,3 @@ +pytest +boto3 +requests diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/__init__.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py new file mode 100644 index 00000000..d98ce574 --- /dev/null +++ b/16-SAM/Practice-16.3/sam-dynamoDb/tests/unit/test_handler.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from hello_world import app + + +@pytest.fixture() +def apigw_event(): + """ Generates API GW Event""" + + return { + "body": '{ "test": "body"}', + "resource": "/{proxy+}", + "requestContext": { + "resourceId": "123456", + "apiId": "1234567890", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "accountId": "123456789012", + "identity": { + "apiKey": "", + "userArn": "", + "cognitoAuthenticationType": "", + "caller": "", + "userAgent": "Custom User Agent String", + "user": "", + "cognitoIdentityPoolId": "", + "cognitoIdentityId": "", + "cognitoAuthenticationProvider": "", + "sourceIp": "127.0.0.1", + "accountId": "", + }, + "stage": "prod", + }, + "queryStringParameters": {"foo": "bar"}, + "headers": { + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "Accept-Language": "en-US,en;q=0.8", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Mobile-Viewer": "false", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "CloudFront-Viewer-Country": "US", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Upgrade-Insecure-Requests": "1", + "X-Forwarded-Port": "443", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "X-Forwarded-Proto": "https", + "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", + "CloudFront-Is-Tablet-Viewer": "false", + "Cache-Control": "max-age=0", + "User-Agent": "Custom User Agent String", + "CloudFront-Forwarded-Proto": "https", + "Accept-Encoding": "gzip, deflate, sdch", + }, + "pathParameters": {"proxy": "/examplepath"}, + "httpMethod": "POST", + "stageVariables": {"baz": "qux"}, + "path": "/examplepath", + } + + +def test_lambda_handler(apigw_event): + + ret = app.lambda_handler(apigw_event, "") + data = json.loads(ret["body"]) + + assert ret["statusCode"] == 200 + assert "message" in ret["body"] + assert data["message"] == "hello world" diff --git a/16-SAM/Practice-16.4/sam-app/.gitignore b/16-SAM/Practice-16.4/sam-app/.gitignore new file mode 100644 index 00000000..eb1db5fb --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/.gitignore @@ -0,0 +1,129 @@ + +# Created by https://www.gitignore.io/api/osx,node,linux,windows + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +# End of https://www.gitignore.io/api/osx,node,linux,windows \ No newline at end of file diff --git a/16-SAM/Practice-16.4/sam-app/README.md b/16-SAM/Practice-16.4/sam-app/README.md new file mode 100644 index 00000000..9bd0bf19 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/README.md @@ -0,0 +1,126 @@ +# sam-app + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello-world - Code for the application's Lambda function. +- events - Invocation events that you can use to invoke the function. +- hello-world/tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. +The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. + +* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) +* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) +* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) +* Node.js - [Install Node.js 10](https://nodejs.org/en/), including the NPM package management tool. +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build +sam deploy --guided +``` + +The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: + +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +* **AWS Region**: The AWS region you want to deploy your app to. +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build` command. + +```bash +sam-app$ sam build +``` + +The SAM CLI installs dependencies defined in `hello-world/package.json`, creates a deployment package, and saves it in the `.aws-sam/build` folder. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +sam-app$ sam local invoke putItemFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +sam-app$ sam local start-api +sam-app$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +sam-app$ sam logs -n HelloWorldFunction --stack-name sam-app --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `hello-world/tests` folder in this project. Use NPM to install the [Mocha test framework](https://mochajs.org/) and run unit tests. + +```bash +sam-app$ cd hello-world +hello-world$ npm install +hello-world$ npm run test +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +aws cloudformation delete-stack --stack-name sam-app +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db b/16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db new file mode 100644 index 00000000..3a99419b Binary files /dev/null and b/16-SAM/Practice-16.4/sam-app/dynamodb/shared-local-instance.db differ diff --git a/16-SAM/Practice-16.4/sam-app/events/event.json b/16-SAM/Practice-16.4/sam-app/events/event.json new file mode 100644 index 00000000..070ad8e0 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore b/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore new file mode 100644 index 00000000..e7e1fb04 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/.npmignore @@ -0,0 +1 @@ +tests/* diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/app.js b/16-SAM/Practice-16.4/sam-app/hello-world/app.js new file mode 100644 index 00000000..4701236a --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/app.js @@ -0,0 +1,33 @@ +// const axios = require('axios') +// const url = 'http://checkip.amazonaws.com/'; +let response; + +/** + * + * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + * @param {Object} event - API Gateway Lambda Proxy Input Format + * + * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html + * @param {Object} context + * + * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + * @returns {Object} object - API Gateway Lambda Proxy Output Format + * + */ +exports.lambdaHandler = async (event, context) => { + try { + // const ret = await axios(url); + response = { + 'statusCode': 200, + 'body': JSON.stringify({ + message: 'hello desmond', + // location: ret.data.trim() + }) + } + } catch (err) { + console.log(err); + return err; + } + + return response +}; diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/package.json b/16-SAM/Practice-16.4/sam-app/hello-world/package.json new file mode 100644 index 00000000..78ddd1ac --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/package.json @@ -0,0 +1,19 @@ +{ + "name": "hello_world", + "version": "1.0.0", + "description": "hello world sample for NodeJS", + "main": "app.js", + "repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs", + "author": "SAM CLI", + "license": "MIT", + "dependencies": { + "axios": "^0.21.1" + }, + "scripts": { + "test": "mocha tests/unit/" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^6.1.4" + } +} diff --git a/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js b/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js new file mode 100644 index 00000000..ae94b9f2 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/hello-world/tests/unit/test-handler.js @@ -0,0 +1,22 @@ +'use strict'; + +const app = require('../../app.js'); +const chai = require('chai'); +const expect = chai.expect; +var event, context; + +describe('Tests index', function () { + it('verifies successful response', async () => { + const result = await app.lambdaHandler(event, context) + + expect(result).to.be.an('object'); + expect(result.statusCode).to.equal(200); + expect(result.body).to.be.an('string'); + + let response = JSON.parse(result.body); + + expect(response).to.be.an('object'); + expect(response.message).to.be.equal("hello world"); + // expect(response.location).to.be.an("string"); + }); +}); diff --git a/16-SAM/Practice-16.4/sam-app/lifecycleHook.js b/16-SAM/Practice-16.4/sam-app/lifecycleHook.js new file mode 100644 index 00000000..e99acacc --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/lifecycleHook.js @@ -0,0 +1,35 @@ +'use strict'; + +const aws = require('aws-sdk'); +const codedeploy = new aws.CodeDeploy({ apiVersion: '2014-10-06' }); + +exports.handler = (event, context, callback) => { + //Read the DeploymentId from the event payload. + var deploymentId = event.DeploymentId; + + //Read the LifecycleEventHookExecutionId from the event payload + var lifecycleEventHookExecutionId = event.LifecycleEventHookExecutionId; + + /* + Enter validation tests here. + */ + + // Prepare the validation test results with the deploymentId and + // the lifecycleEventHookExecutionId for AWS CodeDeploy. + var params = { + deploymentId: deploymentId, + lifecycleEventHookExecutionId: lifecycleEventHookExecutionId, + status: 'Succeeded' // status can be 'Succeeded' or 'Failed' + }; + + // Pass AWS CodeDeploy the prepared validation test results. + codedeploy.putLifecycleEventHookExecutionStatus(params, function (err, data) { + if (err) { + // Validation failed. + callback('Validation test failed'); + } else { + // Validation succeeded. + callback(null, 'Validation test succeeded'); + } + }); +}; \ No newline at end of file diff --git a/16-SAM/Practice-16.4/sam-app/packaged.yaml b/16-SAM/Practice-16.4/sam-app/packaged.yaml new file mode 100644 index 00000000..8387dbc5 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/packaged.yaml @@ -0,0 +1,114 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: 'sam-app + + Sample SAM Template for sam-app + + ' +Globals: + Function: + Timeout: 3 +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-bucket-desmond/7201c45d112e260889aa90899fc0ab5e + Handler: app.lambdaHandler + Runtime: nodejs14.x + Architectures: + - arm64 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery10Minutes + Hooks: + PreTraffic: + Ref: beforeAllowTraffic + PostTraffic: + Ref: afterAllowTraffic + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get + Metadata: + SamResourceId: HelloWorldFunction + beforeAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - codedeploy:PutLifecycleEventHookExecutionStatus + Resource: + Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - lambda:InvokeFunction + Resource: + Ref: HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: CodeDeployHook_beforeAllowTraffic + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: + Ref: HelloWorldFunction.Version + CodeUri: s3://sam-bucket-desmond/c973d46b9711c2fecf426d9ae063c5f5 + Metadata: + SamResourceId: beforeAllowTraffic + afterAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - codedeploy:PutLifecycleEventHookExecutionStatus + Resource: + Fn::Sub: arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/* + - Version: '2012-10-17' + Statement: + - Effect: Allow + Action: + - lambda:InvokeFunction + Resource: + Ref: HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: CodeDeployHook_afterAllowTraffic + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: + Ref: HelloWorldFunction.Version + CodeUri: s3://sam-bucket-desmond/c973d46b9711c2fecf426d9ae063c5f5 + Metadata: + SamResourceId: afterAllowTraffic +Outputs: + HelloWorldApi: + Description: API Gateway endpoint URL for Prod stage for Hello World function + Value: + Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/ + HelloWorldFunction: + Description: Hello World Lambda Function ARN + Value: + Fn::GetAtt: + - HelloWorldFunction + - Arn + HelloWorldFunctionIamRole: + Description: Implicit IAM Role created for Hello World function + Value: + Fn::GetAtt: + - HelloWorldFunctionRole + - Arn diff --git a/16-SAM/Practice-16.4/sam-app/template.yaml b/16-SAM/Practice-16.4/sam-app/template.yaml new file mode 100644 index 00000000..500d0c05 --- /dev/null +++ b/16-SAM/Practice-16.4/sam-app/template.yaml @@ -0,0 +1,101 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-app + + Sample SAM Template for sam-app + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: hello-world/ + Handler: app.lambdaHandler + Runtime: nodejs14.x + Architectures: + - arm64 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery10Minutes + Hooks: + PreTraffic: !Ref beforeAllowTraffic + PostTraffic: !Ref afterAllowTraffic + Events: + HelloWorld: + 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: /hello + Method: get + + beforeAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "codedeploy:PutLifecycleEventHookExecutionStatus" + Resource: + !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "lambda:InvokeFunction" + Resource: !Ref HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: 'CodeDeployHook_beforeAllowTraffic' + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: !Ref HelloWorldFunction.Version + + afterAllowTraffic: + Type: AWS::Serverless::Function + Properties: + Handler: lifecycleHook.handler + Policies: + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "codedeploy:PutLifecycleEventHookExecutionStatus" + Resource: + !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*' + - Version: "2012-10-17" + Statement: + - Effect: "Allow" + Action: + - "lambda:InvokeFunction" + Resource: !Ref HelloWorldFunction.Version + Runtime: nodejs14.x + FunctionName: 'CodeDeployHook_afterAllowTraffic' + DeploymentPreference: + Enabled: false + Timeout: 5 + Environment: + Variables: + NewVersion: !Ref HelloWorldFunction.Version + +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 + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/16-SAM/commands b/16-SAM/commands new file mode 100644 index 00000000..4ea4db20 --- /dev/null +++ b/16-SAM/commands @@ -0,0 +1,14 @@ +sam build && sam package --output-template-file packaged.yaml --s3-bucket sam-bucket-desmond --profile labs +sam deploy --template-file packaged.yaml --capabilities CAPABILITY_IAM \ +--stack-name SAM-DynamoDB --profile labs + + +aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/324320755747/SampleQueue \ + --message-body events/event.json \ + --delay-seconds 10 \ + --profile labs + +aws dynamodb create-table --cli-input-json file://create-table.json \ +--endpoint-url http://localhost:8000 + +aws dynamodb scan --table-name DynamoDBTable --endpoint-url http://localhost:8000 diff --git a/16-SAM/create-table.json b/16-SAM/create-table.json index b9772498..b25872c6 100644 --- a/16-SAM/create-table.json +++ b/16-SAM/create-table.json @@ -1,5 +1,5 @@ { - "TableName": "", + "TableName": "DynamoDBTable", "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ],