├── Lambda Code ├── Node │ └── examples │ │ ├── sdk-exploration │ │ ├── Node SDK Exploration Commands │ │ └── listDragons.js │ │ ├── add-dragon │ │ ├── newDragonPayload.json │ │ ├── Commands.txt │ │ ├── addDragon.js │ │ └── package-lock.json │ │ ├── validate-dragon │ │ ├── newDragonPayload.json │ │ ├── duplicateDragonPayload.json │ │ ├── Commands.txt │ │ ├── validateDragon.js │ │ └── package-lock.json │ │ └── list-dragons │ │ ├── Commands │ │ └── listDragonsLambda.js ├── Python │ ├── add-dragon │ │ ├── newDragonPayload.json │ │ ├── Commands.txt │ │ └── addDragon.py │ ├── validate-dragon │ │ ├── newDragonPayload.json │ │ ├── duplicateDragonPayload.json │ │ ├── Commands.txt │ │ └── validateDragon.py │ ├── list-dragons │ │ ├── Commands.txt │ │ ├── X-Ray Commands.txt │ │ └── listDragons.py │ └── SDK Exploration │ │ ├── client.py │ │ ├── resource.py │ │ └── app.py └── Java │ ├── lambda-demo │ ├── validateDragon │ │ ├── dragon_payload.json │ │ └── validate-dragon │ │ │ ├── build.gradle │ │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── mycompany │ │ │ │ └── app │ │ │ │ ├── DragonValidationException.java │ │ │ │ └── App.java │ │ │ └── pom.xml │ ├── addDragon │ │ ├── dragon_payload.json │ │ └── add-dragon │ │ │ ├── build.gradle │ │ │ ├── pom.xml │ │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── mycompany │ │ │ └── app │ │ │ ├── model │ │ │ └── Dragon.java │ │ │ └── App.java │ └── listDragons │ │ └── my-app │ │ ├── build.gradle │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── mycompany │ │ └── app │ │ └── App.java │ ├── xray-demo │ └── my-app │ │ ├── build.gradle │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── mycompany │ │ └── app │ │ └── App.java │ ├── Commands_SDK_Exploration.txt │ ├── sdkExploration │ └── my-app │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── mycompany │ │ └── app │ │ └── App.java │ └── Commands_Lambda_XRay.txt ├── API Gateway Demo ├── dragon_payload.json ├── ReportDragonMapping.vtl ├── dragon_model.json └── ResponseMappingForMock.vtl ├── Step Functions ├── StartExecutionPayloadExample.json └── ReportDragonStepFunction.json ├── Permissions └── S3BucketPolicy.json ├── README.txt └── Dragon Data └── dragon_stats_one.json /Lambda Code/Node/examples/sdk-exploration/Node SDK Exploration Commands: -------------------------------------------------------------------------------- 1 | Check for current node version 2 | node -v 3 | 4 | Install AWS SDK 5 | npm install aws-sdk 6 | 7 | Run program 8 | node readDragons 9 | 10 | -------------------------------------------------------------------------------- /Lambda Code/Node/examples/add-dragon/newDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Bob is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"Bob", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | } -------------------------------------------------------------------------------- /Lambda Code/Python/add-dragon/newDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"George is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"George", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | } -------------------------------------------------------------------------------- /Lambda Code/Node/examples/validate-dragon/newDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Bob is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"Bob", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | } -------------------------------------------------------------------------------- /Lambda Code/Python/validate-dragon/newDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"George is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"George", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | } -------------------------------------------------------------------------------- /API Gateway Demo/dragon_payload.json: -------------------------------------------------------------------------------- 1 | { 2 | "dragonName": "Frank", 3 | "description": "This dragon is brand new, we don't know much about it yet.", 4 | "family": "purple", 5 | "city": "Seattle", 6 | "country": "USA", 7 | "state": "WA", 8 | "neighborhood": "Downtown", 9 | "reportingPhoneNumber": "15555555555", 10 | "confirmationRequired": false 11 | } -------------------------------------------------------------------------------- /Step Functions/StartExecutionPayloadExample.json: -------------------------------------------------------------------------------- 1 | { 2 | "dragon_name_str": "Greta", 3 | "description_str": "New dragon", 4 | "family_str": "green", 5 | "location_city_str": "Houston", 6 | "location_country_str": "USA", 7 | "location_state_str": "TX", 8 | "location_neighborhood_str": "Downtown", 9 | "reportingPhoneNumber": "15555555555", 10 | "confirmationRequired": false 11 | } -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/validateDragon/dragon_payload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Atlas is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"Atlas", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | 10 | } -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/addDragon/dragon_payload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Morgan is a new dragon, we don't know much about them yet.", 3 | "dragon_name_str":"Morgan Willis", 4 | "family_str":"green", 5 | "location_city_str":"seattle", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"4th st", 8 | "location_state_str":"washington" 9 | 10 | } -------------------------------------------------------------------------------- /Lambda Code/Python/validate-dragon/duplicateDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Shadow is one of the stealthiest dragons. He can disappear and is known to assassinate many of his enemies.", 3 | "dragon_name_str":"Shadow", 4 | "family_str":"black", 5 | "location_city_str":"eden", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"church st", 8 | "location_state_str":"mississsippi" 9 | } -------------------------------------------------------------------------------- /Lambda Code/Node/examples/validate-dragon/duplicateDragonPayload.json: -------------------------------------------------------------------------------- 1 | { 2 | "description_str":"Shadow is one of the stealthiest dragons. He can disappear and is known to assassinate many of his enemies.", 3 | "dragon_name_str":"Shadow", 4 | "family_str":"black", 5 | "location_city_str":"eden", 6 | "location_country_str":"usa", 7 | "location_neighborhood_str":"church st", 8 | "location_state_str":"mississsippi" 9 | } -------------------------------------------------------------------------------- /Lambda Code/Node/examples/list-dragons/Commands: -------------------------------------------------------------------------------- 1 | Create new folder 2 | Copy code into folder 3 | 4 | Download SDK to deployment package folder 5 | npm install --prefix . aws-sdk 6 | 7 | Zip up deployment package 8 | zip -r ../nodeListDragonsFunction.zip . 9 | 10 | Create Lambda Function 11 | aws lambda create-function --function-name list-dragons --runtime nodejs10.x --role --handler listDragonsLambda.handler --publish --zip-file fileb://nodeListDragonsFunction.zip 12 | 13 | Invoke Function 14 | aws lambda invoke --function-name list-dragons output.txt 15 | -------------------------------------------------------------------------------- /API Gateway Demo/ReportDragonMapping.vtl: -------------------------------------------------------------------------------- 1 | #set($data = $input.path('$')) 2 | 3 | #set($input = " { ""dragon_name_str"" : ""$data.dragonName"", ""description_str"" : ""$data.description"", ""family_str"" : ""$data.family"", ""location_city_str"" : ""$data.city"", ""location_country_str"" : ""$data.country"", ""location_state_str"" : ""$data.state"", ""location_neighborhood_str"" : ""$data.neighborhood"", ""reportingPhoneNumber"" : ""$data.reportingPhoneNumber"", ""confirmationRequired"" : $data.confirmationRequired}") 4 | 5 | 6 | { 7 | "input": "$util.escapeJavaScript($input).replaceAll("\\'", "'")", 8 | 9 | "stateMachineArn": "" 10 | } -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/validateDragon/validate-dragon/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | compile ( 9 | 'com.amazonaws:aws-lambda-java-core:1.2.0', 10 | 'com.amazonaws:aws-java-sdk-s3:1.11.749', 11 | 'com.amazonaws:aws-java-sdk-ssm:1.11.749', 12 | 'com.amazonaws:aws-lambda-java-events:2.2.2', 13 | 'commons-io:commons-io:+' 14 | ) 15 | } 16 | 17 | task buildZip(type: Zip) { 18 | from compileJava 19 | from processResources 20 | into('lib') { 21 | from configurations.runtimeClasspath 22 | } 23 | } 24 | 25 | build.dependsOn buildZip -------------------------------------------------------------------------------- /Permissions/S3BucketPolicy.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Sid": "AllowS3ReadAccess", 6 | "Effect": "Allow", 7 | "Principal": { 8 | "AWS": [ 9 | "", 10 | "", 11 | "" 12 | ] 13 | }, 14 | "Action": "s3:Get*", 15 | "Resource": [ 16 | "arn:aws:s3:::", 17 | "arn:aws:s3:::/*" 18 | ] 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /Lambda Code/Node/examples/add-dragon/Commands.txt: -------------------------------------------------------------------------------- 1 | Install the AWS SDK into the folder 2 | npm install --prefix . aws-sdk 3 | 4 | Zip up your code 5 | zip -r ../nodeAddDragonFunction.zip . 6 | 7 | Create Lambda Function 8 | 9 | aws lambda create-function --function-name add-dragon --runtime nodejs10.x --role --handler addDragon.handler --publish --zip-file fileb://nodeAddDragonFunction.zip 10 | 11 | Invoke 12 | 13 | aws lambda invoke --function-name add-dragon addOutput.txt --payload file://newDragonPayload.json 14 | 15 | Update Lambda Code 16 | aws lambda update-function-code --function-name add-dragon --zip-file fileb://nodeAddDragonFunction.zip --publish -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/listDragons/my-app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | compile ( 9 | 'com.amazonaws:aws-lambda-java-core:1.2.0', 10 | 'com.amazonaws:aws-java-sdk-s3:1.11.749', 11 | 'com.amazonaws:aws-java-sdk-ssm:1.11.749', 12 | 'com.amazonaws:aws-lambda-java-events:2.2.2', 13 | 'commons-io:commons-io:+', 14 | 'com.google.code.gson:gson:2.8.6' 15 | ) 16 | } 17 | 18 | task buildZip(type: Zip) { 19 | from compileJava 20 | from processResources 21 | into('lib') { 22 | from configurations.runtimeClasspath 23 | } 24 | } 25 | 26 | build.dependsOn buildZip -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/addDragon/add-dragon/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | compile ( 9 | 'com.amazonaws:aws-lambda-java-core:1.2.0', 10 | 'com.amazonaws:aws-java-sdk-s3:1.11.749', 11 | 'com.amazonaws:aws-java-sdk-ssm:1.11.749', 12 | 'com.amazonaws:aws-lambda-java-events:2.2.2', 13 | 'com.google.code.gson:gson:2.8.6', 14 | 'commons-io:commons-io:+' 15 | ) 16 | } 17 | 18 | task buildZip(type: Zip) { 19 | from compileJava 20 | from processResources 21 | into('lib') { 22 | from configurations.runtimeClasspath 23 | } 24 | } 25 | 26 | build.dependsOn buildZip -------------------------------------------------------------------------------- /API Gateway Demo/dragon_model.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "title": "Dragon", 4 | "type": "object", 5 | "properties": { 6 | "dragonName": { 7 | "type": "string" 8 | }, 9 | "description": { 10 | "type": "string" 11 | }, 12 | "family": { 13 | "type": "string" 14 | }, 15 | "city": { 16 | "type": "string" 17 | }, 18 | "country": { 19 | "type": "string" 20 | }, 21 | "state": { 22 | "type": "string" 23 | }, 24 | "neighborhood": { 25 | "type": "string" 26 | }, 27 | "reportingPhoneNumber": { 28 | "type": "string" 29 | }, 30 | "confirmationRequired": { 31 | "type": "boolean" 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Lambda Code/Python/list-dragons/Commands.txt: -------------------------------------------------------------------------------- 1 | Download dependencies into folder 2 | sudo pip install --target ./list-dragons-package boto3 3 | 4 | Zip up your code (with dependencies) 5 | zip -r9 ${OLDPWD}/ pythonlistDragonsFunction.zip . 6 | 7 | Add python script to zip 8 | zip -g pythonlistDragonsFunction.zip listDragons.py 9 | 10 | Create Lambda Function 11 | aws lambda create-function --function-name list-dragons --runtime python3.6 --role --handler listDragons.listDragons --publish --zip-file fileb://pythonlistDragonsFunction.zip 12 | 13 | Invoke Lambda Function 14 | aws lambda invoke --function-name list-dragons output.txt 15 | 16 | Update Lambda Code 17 | aws lambda update-function-code --function-name list-dragons --zip-file fileb://pythonlistDragonsFunction.zip --publish -------------------------------------------------------------------------------- /Lambda Code/Node/examples/validate-dragon/Commands.txt: -------------------------------------------------------------------------------- 1 | Create Folder, copy js file into folder and run the following commands: 2 | 3 | Install the AWS SDK into the folder 4 | npm install --prefix . aws-sdk 5 | 6 | Zip up your code 7 | zip -r ../nodeValidateDragonFunction.zip . 8 | 9 | Create Lambda Function 10 | aws lambda create-function --function-name validate-dragon --runtime nodejs10.x --role --handler validateDragon.handler --publish --zip-file fileb://nodeValidateDragonFunction.zip 11 | 12 | 13 | Invoke 14 | aws lambda invoke --function-name validate-dragon validateOutput.txt --payload file://newDragonPayload.json 15 | 16 | 17 | 18 | Update Lambda Code 19 | aws lambda update-function-code --function-name validate-dragon --zip-file fileb://nodeValidateDragonFunction.zip --publish 20 | -------------------------------------------------------------------------------- /Lambda Code/Python/add-dragon/Commands.txt: -------------------------------------------------------------------------------- 1 | Download dependencies into folder 2 | sudo pip install --target ./add-dragon-package boto3 3 | 4 | Zip up your code (with dependencies) 5 | zip -r9 ${OLDPWD}/ pythonAddDragonFunction.zip . 6 | 7 | Add python script to zip 8 | zip -g pythonAddDragonFunction.zip addDragon.py 9 | 10 | Create Lambda Function 11 | aws lambda create-function --function-name add-dragon --runtime python3.6 --role --handler addDragon.addDragonToFile --publish --zip-file fileb://pythonAddDragonFunction.zip 12 | 13 | Invoke Lambda Function 14 | aws lambda invoke --function-name add-dragon output.txt --payload file://newDragonPayload.json 15 | 16 | Update Lambda Code 17 | aws lambda update-function-code --function-name add-dragon --zip-file fileb://pythonAddDragonFunction.zip --publish -------------------------------------------------------------------------------- /Lambda Code/Java/xray-demo/my-app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | compile ( 9 | 'com.amazonaws:aws-lambda-java-core:1.2.0', 10 | 'com.amazonaws:aws-java-sdk-s3:1.11.749', 11 | 'com.amazonaws:aws-java-sdk-ssm:1.11.749', 12 | 'com.amazonaws:aws-lambda-java-events:2.2.2', 13 | 'commons-io:commons-io:+', 14 | 'com.google.code.gson:gson:2.8.6', 15 | 'com.amazonaws:aws-xray-recorder-sdk-core:2.4.0', 16 | 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk:2.4.0', 17 | 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-instrumentor:2.4.0' 18 | ) 19 | } 20 | 21 | task buildZip(type: Zip) { 22 | from compileJava 23 | from processResources 24 | into('lib') { 25 | from configurations.runtimeClasspath 26 | } 27 | } 28 | 29 | build.dependsOn buildZip -------------------------------------------------------------------------------- /Lambda Code/Python/validate-dragon/Commands.txt: -------------------------------------------------------------------------------- 1 | Download dependencies into folder 2 | sudo pip install --target ./validate-dragon-package boto3 3 | 4 | Zip up your code (with dependencies) 5 | zip -r9 ${OLDPWD}/ pythonValidateDragonFunction.zip . 6 | 7 | Add python script to zip 8 | zip -g pythonValidateDragonFunction.zip validateDragon.py 9 | 10 | Create Lambda Function 11 | aws lambda create-function --function-name validate-dragon --runtime python3.6 --role --handler validateDragon.validate --publish --zip-file fileb://pythonValidateDragonFunction.zip 12 | 13 | Invoke Lambda Function 14 | aws lambda invoke --function-name validate-dragon output.txt --payload file://duplicateDragonPayload.json 15 | 16 | Update Lambda Code 17 | aws lambda update-function-code --function-name validate-dragon --zip-file fileb://pythonValidateDragonFunction.zip --publish -------------------------------------------------------------------------------- /Lambda Code/Java/Commands_SDK_Exploration.txt: -------------------------------------------------------------------------------- 1 | Check Java Version 2 | java -version 3 | 4 | Update Java Version 5 | sudo yum install java-1.8.0-openjdk-devel 6 | 7 | Configure Java 8 | sudo alternatives --config java 9 | 10 | Remove Old Java Version 11 | sudo yum remove java-1.7.0-openjdk-devel 12 | 13 | Check Version 14 | java -version 15 | 16 | Download Maven 17 | sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo 18 | sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo 19 | 20 | Install Apache Maven 21 | sudo yum install -y apache-maven 22 | 23 | Chevk Maven Version 24 | mvn -v 25 | 26 | Set Java Home 27 | export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.51.amzn1.x86_64 28 | 29 | Check Maven Version 30 | mvn -v 31 | 32 | -------------------------------------------------------------------------------- /Lambda Code/Python/list-dragons/X-Ray Commands.txt: -------------------------------------------------------------------------------- 1 | Download X-Ray SDK 2 | sudo pip install aws-xray-sdk 3 | 4 | Import for python script 5 | from aws_xray_sdk.core import patch_all 6 | 7 | Call patch all method 8 | patch_all() 9 | 10 | Download Xray SDK to target 11 | sudo pip install --target ./list-dragons-package aws-xray-sdk 12 | sudo pip install --target ./list-dragons-package boto3 13 | 14 | Zip up package folder 15 | zip -r9 ${OLDPWD}/listDragonsPythonFunction.zip . 16 | 17 | Add python script to zip 18 | zip -g listDragonsPythonFunction.zip listDragons.py 19 | 20 | Update Function Code 21 | aws lambda update-function-code --function-name list-dragons --zip-file fileb://listDragonsPythonFunction.zip --publish 22 | 23 | Update Function Configuration 24 | aws lambda update-function-configuration --function-name list-dragons --tracing-config Mode=Active 25 | 26 | 27 | -------------------------------------------------------------------------------- /Lambda Code/Python/SDK Exploration/client.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | 14 | client = boto3.client('s3') 15 | 16 | response = client.list_objects(Bucket='') 17 | 18 | for content in response['Contents']: 19 | obj_dict = client.get_object(Bucket='', Key=content['Key']) 20 | print(content['Key'], obj_dict['LastModified']) 21 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/validateDragon/validate-dragon/src/main/java/com/mycompany/app/DragonValidationException.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | public class DragonValidationException extends RuntimeException { 15 | public DragonValidationException(String errorMessage, Throwable err) { 16 | super(errorMessage, err); 17 | } 18 | } -------------------------------------------------------------------------------- /Lambda Code/Python/SDK Exploration/resource.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | 14 | s3 = boto3.resource('s3') 15 | bucket = s3.Bucket('') 16 | # the objects are available as a collection on the bucket object 17 | for obj in bucket.objects.all(): 18 | print(obj.key, obj.last_modified) 19 | 20 | # access the client from the resource 21 | s3_client = boto3.resource('s3').meta.client -------------------------------------------------------------------------------- /Lambda Code/Java/sdkExploration/my-app/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | my-app.1 6 | jar 7 | 1.0-SNAPSHOT 8 | my-app 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.1 15 | test 16 | 17 | 18 | com.amazonaws 19 | aws-java-sdk 20 | 1.11.749 21 | 22 | 23 | commons-io 24 | commons-io 25 | 2.5 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | Resources needed in your AWS account before beginning the demos: 13 | 14 | 1. An S3 bucket 15 | a. You can find an example of the bucket policy in the Permissions folder 16 | 2. IAM Roles for the lambda functions 17 | a. Policies to attach 18 | i. LambdaBasicExecutionRole 19 | ii. AWSXrayFullAccess 20 | iii. AmazonSSMFullAccess or more tightly scoped custom policy 21 | iiii. AmazonS3FullAccess or more tightly scoped custom policy 22 | 3. Parameters in AWS Systems Manager Parameter Store 23 | a. One parameter for the bucket name, unencrypted 24 | i. Use dragon_data_bucket_name for the name of the parameter 25 | b. One parameter for the key name, unencrypted 26 | i. Use dragon_data_file_name for the name of the parameter 27 | -------------------------------------------------------------------------------- /Lambda Code/Python/SDK Exploration/app.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | 14 | s3 = boto3.resource('s3','us-east-1').meta.client 15 | ssm = boto3.client('ssm', 'us-east-1') 16 | bucket_name = ssm.get_parameter( Name='dragon_data_bucket_name',WithDecryption=False)['Parameter']['Value'] 17 | file_name = ssm.get_parameter( Name='dragon_data_file_name',WithDecryption=False)['Parameter']['Value'] 18 | 19 | def listDragons(): 20 | 21 | expression = "select * from s3object s" 22 | 23 | result = s3.select_object_content( 24 | Bucket=bucket_name, 25 | Key=file_name, 26 | ExpressionType='SQL', 27 | Expression=expression, 28 | InputSerialization={'JSON': {'Type': 'Document'}}, 29 | OutputSerialization={'JSON': {}} 30 | ) 31 | 32 | for event in result['Payload']: 33 | if 'Records' in event: 34 | print(event['Records']['Payload'].decode('utf-8')) 35 | 36 | listDragons() -------------------------------------------------------------------------------- /Lambda Code/Java/Commands_Lambda_XRay.txt: -------------------------------------------------------------------------------- 1 | Make sure Java Home is set (you might need to change this if you are using a different version of java) 2 | export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk 3 | 4 | Install Gradle if it's not installed 5 | sdk install gradle 6.4.1 6 | 7 | Create Zip 8 | gradle build 9 | 10 | Create Lambda Function 11 | aws lambda create-function --function-name listDragons --runtime java8 --role --handler com.mycompany.app.App::handleRequest --publish --zip-file fileb://my-app.zip --timeout 90 --memory-size 448 12 | aws lambda create-function --function-name validateDragon --runtime java8 --role --handler com.mycompany.app.App::handleRequest --publish --zip-file fileb://validate-dragon.zip --timeout 90 --memory-size 448 13 | aws lambda create-function --function-name addDragon --runtime java8 --role --handler com.mycompany.app.App::handleRequest --publish --zip-file fileb://add-dragon.zip --timeout 90 --memory-size 448 14 | 15 | Invoke Lambda Function 16 | aws lambda invoke --function-name listDragons output.txt 17 | aws lambda invoke --function-name validateDragon output.txt --payload file://dragon_payload.json 18 | aws lambda invoke --function-name addDragon output.txt --payload file://dragon_payload.json 19 | 20 | 21 | Update Lambda Function 22 | aws lambda update-function-code --function-name validateDragon --publish --zip-file fileb://validate-dragon.zip 23 | aws lambda update-function-code --function-name addDragon --publish --zip-file fileb://add-dragon.zip 24 | aws lambda update-function-code --function-name listDragons --publish --zip-file fileb://my-app.zip 25 | -------------------------------------------------------------------------------- /Lambda Code/Python/add-dragon/addDragon.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | import json 14 | 15 | s3 = boto3.client('s3') 16 | ssm = boto3.client('ssm', 'us-east-1') 17 | bucket_name = ssm.get_parameter( Name='dragon_data_bucket_name',WithDecryption=False)['Parameter']['Value'] 18 | file_name = ssm.get_parameter( Name='dragon_data_file_name',WithDecryption=False)['Parameter']['Value'] 19 | 20 | def addDragonToFile(event, context): 21 | 22 | dragon_data = { 23 | "description_str":event['description_str'], 24 | "dragon_name_str":event['dragon_name_str'], 25 | "family_str":event['family_str'], 26 | "location_city_str":event['location_city_str'], 27 | "location_country_str":event['location_country_str'], 28 | "location_neighborhood_str":event['location_neighborhood_str'], 29 | "location_state_str":event['location_state_str'] 30 | } 31 | 32 | resp=s3.get_object(Bucket=bucket_name, Key=file_name) 33 | data=resp.get('Body').read() 34 | 35 | json_data = json.loads(data) 36 | json_data.append(dragon_data) 37 | s3.put_object(Bucket=bucket_name, Key=file_name, Body=json.dumps(json_data).encode()) 38 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/addDragon/add-dragon/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | add-dragon 6 | jar 7 | 1.0-SNAPSHOT 8 | add-dragon 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.1 15 | test 16 | 17 | 18 | com.google.code.gson 19 | gson 20 | 2.8.6 21 | 22 | 23 | com.amazonaws 24 | aws-java-sdk-ssm 25 | 1.11.749 26 | 27 | 28 | com.amazonaws 29 | aws-java-sdk-s3 30 | 1.11.749 31 | 32 | 33 | commons-io 34 | commons-io 35 | 2.5 36 | 37 | 38 | com.amazonaws 39 | aws-lambda-java-core 40 | 1.2.0 41 | 42 | 43 | com.amazonaws 44 | aws-lambda-java-events 45 | 2.2.2 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/listDragons/my-app/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | my-app.1 6 | jar 7 | 1.0-SNAPSHOT 8 | my-app 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.1 15 | test 16 | 17 | 18 | com.amazonaws 19 | aws-java-sdk-ssm 20 | 1.11.749 21 | 22 | 23 | com.amazonaws 24 | aws-java-sdk-s3 25 | 1.11.749 26 | 27 | 28 | commons-io 29 | commons-io 30 | 2.5 31 | 32 | 33 | com.google.code.gson 34 | gson 35 | 2.8.6 36 | 37 | 38 | com.amazonaws 39 | aws-lambda-java-core 40 | 1.2.0 41 | 42 | 43 | com.amazonaws 44 | aws-lambda-java-events 45 | 2.2.2 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/validateDragon/validate-dragon/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | validate-dragon 6 | jar 7 | 1.0-SNAPSHOT 8 | validate-dragon 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.1 15 | test 16 | 17 | 18 | com.amazonaws 19 | aws-java-sdk-ssm 20 | 1.11.749 21 | 22 | 23 | com.amazonaws 24 | aws-java-sdk-s3 25 | 1.11.749 26 | 27 | 28 | commons-io 29 | commons-io 30 | 2.5 31 | 32 | 33 | com.google.code.gson 34 | gson 35 | 2.8.6 36 | 37 | 38 | com.amazonaws 39 | aws-lambda-java-core 40 | 1.2.0 41 | 42 | 43 | com.amazonaws 44 | aws-lambda-java-events 45 | 2.2.2 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lambda Code/Python/validate-dragon/validateDragon.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | import json 14 | 15 | s3 = boto3.client('s3','us-east-1') 16 | ssm = boto3.client('ssm', 'us-east-1') 17 | bucket_name = ssm.get_parameter( Name='dragon_data_bucket_name',WithDecryption=False)['Parameter']['Value'] 18 | file_name = ssm.get_parameter( Name='dragon_data_file_name',WithDecryption=False)['Parameter']['Value'] 19 | 20 | def validate(event, context): 21 | 22 | result = s3.select_object_content( 23 | Bucket=bucket_name, 24 | Key=file_name, 25 | ExpressionType='SQL', 26 | Expression="select * from S3Object[*][*] s where s.dragon_name_str = '" + event['dragon_name_str'] + "'", 27 | InputSerialization={'JSON': {'Type': 'Document'}}, 28 | OutputSerialization={'JSON': {}} 29 | ) 30 | 31 | for records in result['Payload']: 32 | if 'Records' in records: 33 | raise DragonValidationException("Duplicate dragon reported") 34 | return 'Dragon Validated' 35 | 36 | class DragonValidationException(Exception): 37 | def __init__(self, *args): 38 | if args: 39 | self.message = args[0] 40 | else: 41 | self.message = None 42 | 43 | def __str__(self): 44 | if self.message: 45 | return 'DragonValidationException, {0}'.format(self.message) 46 | else: 47 | return 'DragonValidationException has been raised' 48 | -------------------------------------------------------------------------------- /Lambda Code/Python/list-dragons/listDragons.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | # in compliance with the License. A copy of the License is located at 5 | # 6 | # https://aws.amazon.com/apache-2-0/ 7 | # 8 | # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | # specific language governing permissions and limitations under the License. 11 | 12 | import boto3 13 | import json 14 | from aws_xray_sdk.core import patch_all 15 | 16 | patch_all() 17 | 18 | s3 = boto3.client('s3','us-east-1') 19 | ssm = boto3.client('ssm', 'us-east-1') 20 | bucket_name = ssm.get_parameter( Name='dragon_data_bucket_name',WithDecryption=False)['Parameter']['Value'] 21 | file_name = ssm.get_parameter( Name='dragon_data_file_name',WithDecryption=False)['Parameter']['Value'] 22 | 23 | def listDragons(event, context): 24 | 25 | expression = "select * from s3object s" 26 | 27 | if 'queryStringParameters' in event and event['queryStringParameters'] is not None: 28 | if 'dragonName' in event['queryStringParameters']: 29 | expression = "select * from S3Object[*][*] s where s.dragon_name_str = '" + event["queryStringParameters"]['dragonName'] + "'" 30 | if 'family' in event['queryStringParameters']: 31 | expression = "select * from S3Object[*][*] s where s.family_str = '" + event["queryStringParameters"]['family'] + "'" 32 | 33 | result = s3.select_object_content( 34 | Bucket=bucket_name, 35 | Key=file_name, 36 | ExpressionType='SQL', 37 | Expression=expression, 38 | InputSerialization={'JSON': {'Type': 'Document'}}, 39 | OutputSerialization={'JSON': {}} 40 | ) 41 | 42 | records = '' 43 | for event in result['Payload']: 44 | if 'Records' in event: 45 | records = event['Records']['Payload'].decode('utf-8') 46 | 47 | return { 48 | "statusCode": 200, 49 | "body": json.dumps(records) 50 | } 51 | -------------------------------------------------------------------------------- /Step Functions/ReportDragonStepFunction.json: -------------------------------------------------------------------------------- 1 | { 2 | "Comment": "Dragon will be validated, if it fails it will send a failure message. If the dragon is valid it will be added to the data and a success message will be sent", 3 | "StartAt": "ValidateDragon", 4 | "States": { 5 | "ValidateDragon": { 6 | "Type": "Task", 7 | "Resource": "", 8 | "Catch": [ 9 | { 10 | "ErrorEquals": [ 11 | "DragonValidationException" 12 | ], 13 | "Next": "AlertDragonValidationFailure", 14 | "ResultPath": null 15 | }, 16 | { 17 | "ErrorEquals": [ 18 | "States.ALL" 19 | ], 20 | "Next": "CatchAllFailure" 21 | } 22 | ], 23 | "Next": "AddDragon", 24 | "ResultPath": null 25 | }, 26 | "AlertDragonValidationFailure": { 27 | "Type": "Task", 28 | "Resource": "arn:aws:states:::sns:publish", 29 | "Parameters": { 30 | "Message": "The dragon you reported failed validation and was not added.", 31 | "PhoneNumber.$": "$.reportingPhoneNumber" 32 | }, 33 | "End": true 34 | }, 35 | "CatchAllFailure": { 36 | "Type": "Fail", 37 | "Cause": "Something unknown went wrong" 38 | }, 39 | "AddDragon": { 40 | "Type": "Task", 41 | "Resource": "", 42 | "Next": "ConfirmationRequired", 43 | "ResultPath": null 44 | }, 45 | "ConfirmationRequired": { 46 | "Type": "Choice", 47 | "Choices": [ 48 | { 49 | "Variable": "$.confirmationRequired", 50 | "BooleanEquals": true, 51 | "Next": "AlertAddDragonSuccess" 52 | }, 53 | { 54 | "Variable": "$.confirmationRequired", 55 | "BooleanEquals": false, 56 | "Next": "NoAlertAddDragonSuccess" 57 | } 58 | ], 59 | "Default": "CatchAllFailure" 60 | }, 61 | "AlertAddDragonSuccess": { 62 | "Type": "Task", 63 | "Resource": "arn:aws:states:::sns:publish", 64 | "Parameters": { 65 | "Message": "The dragon you reported has been added!", 66 | "PhoneNumber.$": "$.reportingPhoneNumber" 67 | }, 68 | "End": true 69 | }, 70 | "NoAlertAddDragonSuccess": { 71 | "Type": "Succeed" 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /Lambda Code/Java/xray-demo/my-app/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.mycompany.app 5 | my-app.1 6 | jar 7 | 1.0-SNAPSHOT 8 | my-app 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.1 15 | test 16 | 17 | 18 | com.amazonaws 19 | aws-java-sdk-ssm 20 | 1.11.749 21 | 22 | 23 | com.amazonaws 24 | aws-xray-recorder-sdk-core 25 | 2.4.0 26 | 27 | 28 | com.amazonaws 29 | aws-xray-recorder-sdk-aws-sdk 30 | 2.4.0 31 | 32 | 33 | com.amazonaws 34 | aws-xray-recorder-sdk-aws-sdk-instrumentor 35 | 2.4.0 36 | 37 | 38 | com.amazonaws 39 | aws-java-sdk-s3 40 | 1.11.749 41 | 42 | 43 | commons-io 44 | commons-io 45 | 2.5 46 | 47 | 48 | com.google.code.gson 49 | gson 50 | 2.8.6 51 | 52 | 53 | com.amazonaws 54 | aws-lambda-java-core 55 | 1.2.0 56 | 57 | 58 | com.amazonaws 59 | aws-lambda-java-events 60 | 2.2.2 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/addDragon/add-dragon/src/main/java/com/mycompany/app/model/Dragon.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app.model; 13 | 14 | public class Dragon { 15 | 16 | String description_str; 17 | String dragon_name_str; 18 | String family_str; 19 | String location_city_str; 20 | String location_neighborhood_str; 21 | String location_state_str; 22 | 23 | public String getDescription_str() { 24 | return description_str; 25 | } 26 | 27 | public void setDescription_str(String description_str) { 28 | this.description_str = description_str; 29 | } 30 | 31 | public String getDragon_name_str() { 32 | return dragon_name_str; 33 | } 34 | 35 | public void setDragon_name_str(String dragon_name_str) { 36 | this.dragon_name_str = dragon_name_str; 37 | } 38 | 39 | public String getFamily_str() { 40 | return family_str; 41 | } 42 | 43 | public void setFamily_str(String family_str) { 44 | this.family_str = family_str; 45 | } 46 | 47 | public String getLocation_city_str() { 48 | return location_city_str; 49 | } 50 | 51 | public void setLocation_city_str(String location_city_str) { 52 | this.location_city_str = location_city_str; 53 | } 54 | 55 | public String getLocation_neighborhood_str() { 56 | return location_neighborhood_str; 57 | } 58 | 59 | public void setLocation_neighborhood_str(String location_neighborhood_str) { 60 | this.location_neighborhood_str = location_neighborhood_str; 61 | } 62 | 63 | public String getLocation_state_str() { 64 | return location_state_str; 65 | } 66 | 67 | public void setLocation_state_str(String location_state_str) { 68 | this.location_state_str = location_state_str; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /Lambda Code/Node/examples/sdk-exploration/listDragons.js: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | var AWS = require("aws-sdk"); 13 | 14 | const s3 = new AWS.S3({ 15 | region: 'us-east-1' 16 | }); 17 | 18 | const ssm = new AWS.SSM({ 19 | region: 'us-east-1' 20 | }); 21 | 22 | async function readDragons() { 23 | var fileName = await getFileName(); 24 | var bucketName = await getBucketName(); 25 | return readDragonsFromS3(bucketName, fileName); 26 | 27 | } 28 | 29 | async function getFileName() { 30 | 31 | var fileNameParams = { 32 | Name: 'dragon_data_file_name', 33 | WithDecryption: false 34 | }; 35 | var promise = await ssm.getParameter(fileNameParams).promise(); 36 | return promise.Parameter.Value; 37 | 38 | } 39 | 40 | async function getBucketName() { 41 | var bucketNameParams = { 42 | Name: 'dragon_data_bucket_name', 43 | WithDecryption: false 44 | }; 45 | 46 | var promise = await ssm.getParameter(bucketNameParams).promise(); 47 | return promise.Parameter.Value; 48 | 49 | } 50 | 51 | function readDragonsFromS3(bucketName, fileName) { 52 | 53 | s3.selectObjectContent({ 54 | Bucket: bucketName, 55 | Expression: 'select * from s3object s', 56 | ExpressionType: 'SQL', 57 | Key: fileName, 58 | InputSerialization: { 59 | JSON: { 60 | Type: 'DOCUMENT', 61 | } 62 | }, 63 | OutputSerialization: { 64 | JSON: { 65 | RecordDelimiter: ',' 66 | } 67 | } 68 | }, function(err, data) { 69 | if (err) { 70 | console.log(err); 71 | } else { 72 | handleData(data); 73 | } 74 | } 75 | ); 76 | } 77 | 78 | function handleData(data) { 79 | data.Payload.on('data', (event) => { 80 | if (event.Records) { 81 | console.log(event.Records.Payload.toString()); 82 | } 83 | }); 84 | } 85 | 86 | readDragons(); -------------------------------------------------------------------------------- /Lambda Code/Node/examples/validate-dragon/validateDragon.js: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | var AWS = require("aws-sdk"); 13 | 14 | const s3 = new AWS.S3({ 15 | region: 'us-east-1' 16 | }); 17 | 18 | const ssm = new AWS.SSM({ 19 | region: 'us-east-1' 20 | }); 21 | 22 | 23 | exports.handler = function(event, context, callback) { 24 | readDragons(event, callback); 25 | } 26 | 27 | async function readDragons(event, callback) { 28 | var fileName = await getFileName(); 29 | var bucketName = await getBucketName(); 30 | var dragonData = readDragonsFromS3(bucketName, fileName, event, callback); 31 | } 32 | 33 | async function getFileName() { 34 | var fileNameParams = { 35 | Name: 'dragon_data_file_name', 36 | WithDecryption: false 37 | }; 38 | var promise = await ssm.getParameter(fileNameParams).promise(); 39 | return promise.Parameter.Value; 40 | } 41 | 42 | async function getBucketName() { 43 | var bucketNameParams = { 44 | Name: 'dragon_data_bucket_name', 45 | WithDecryption: false 46 | }; 47 | var promise = await ssm.getParameter(bucketNameParams).promise(); 48 | return promise.Parameter.Value; 49 | } 50 | 51 | function readDragonsFromS3(bucketName, fileName, event, callback) { 52 | s3.selectObjectContent({ 53 | Bucket: bucketName, 54 | Expression: "select * from S3Object[*][*] s where s.dragon_name_str = '" + event.dragon_name_str + "'", 55 | ExpressionType: 'SQL', 56 | Key: fileName, 57 | InputSerialization: { 58 | JSON: { 59 | Type: 'DOCUMENT', 60 | } 61 | }, 62 | OutputSerialization: { 63 | JSON: { 64 | RecordDelimiter: ',' 65 | } 66 | } 67 | }, function(err, data) { 68 | if (err) { 69 | console.log(err); 70 | } else { 71 | return handleData(data, callback); 72 | } 73 | }); 74 | } 75 | 76 | function handleData(data, callback) { 77 | data.Payload.on('data', (event) => { 78 | if (event.Records) { 79 | callback(new DragonValidationException("Duplicate dragon reported")) 80 | } else { 81 | callback(null, "Dragon Validated") 82 | } 83 | }); 84 | } 85 | 86 | class DragonValidationException extends Error { 87 | constructor (message) { 88 | super(message) 89 | this.name = this.constructor.name 90 | } 91 | } -------------------------------------------------------------------------------- /Lambda Code/Node/examples/add-dragon/addDragon.js: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | var AWS = require("aws-sdk"); 13 | 14 | const s3 = new AWS.S3({ 15 | region: 'us-east-1' 16 | }); 17 | 18 | const ssm = new AWS.SSM({ 19 | region: 'us-east-1' 20 | }); 21 | 22 | 23 | exports.handler = function(event, context, callback) { 24 | 25 | addDragon(event,callback); 26 | 27 | } 28 | 29 | async function addDragon(event, callback) { 30 | 31 | var fileName = await getFileName(); 32 | var bucketName = await getBucketName(); 33 | var dragonData = await addDragonToData(bucketName, fileName, event); 34 | writeToS3(dragonData, bucketName,fileName,callback); 35 | 36 | } 37 | 38 | async function getFileName() { 39 | 40 | var fileNameParams = { 41 | Name: 'dragon_data_file_name', 42 | WithDecryption: false 43 | }; 44 | var promise = await ssm.getParameter(fileNameParams).promise(); 45 | return promise.Parameter.Value; 46 | 47 | } 48 | 49 | async function getBucketName() { 50 | var bucketNameParams = { 51 | Name: 'dragon_data_bucket_name', 52 | WithDecryption: false 53 | }; 54 | 55 | var promise = await ssm.getParameter(bucketNameParams).promise(); 56 | return promise.Parameter.Value; 57 | 58 | } 59 | 60 | async function addDragonToData(bucketName, fileName, event) { 61 | var objectParams = { 62 | Bucket: bucketName, 63 | Key: fileName 64 | }; 65 | 66 | var dragonData = { 67 | "description_str": event.description_str, 68 | "dragon_name_str": event.dragon_name_str, 69 | "family_str": event.family_str, 70 | "location_city_str": event.location_city_str, 71 | "location_country_str": event.location_country_str, 72 | "location_neighborhood_str": event.location_neighborhood_str, 73 | "location_state_str": event.location_state_str 74 | } 75 | 76 | var jsonData=''; 77 | var promise = await s3.getObject(objectParams).promise(); 78 | jsonData = JSON.parse(promise.Body.toString()); 79 | jsonData.push(dragonData); 80 | return jsonData; 81 | 82 | } 83 | 84 | function writeToS3(dragonData, bucketName, fileName, callback) { 85 | 86 | var uploadParams = { 87 | Bucket: bucketName, 88 | Key: fileName, 89 | Body: JSON.stringify(dragonData) 90 | } 91 | 92 | s3.upload(uploadParams, function (err, data) { 93 | if (data) { 94 | callback(null, { 95 | "statusCode" : 200, 96 | }) 97 | } 98 | if(err) { 99 | callback("Error" + err.message) 100 | } 101 | }) 102 | 103 | } 104 | 105 | 106 | -------------------------------------------------------------------------------- /Lambda Code/Node/examples/list-dragons/listDragonsLambda.js: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | var AWS = require('aws-sdk'); 13 | 14 | const s3 = new AWS.S3({ 15 | region: 'us-east-1' 16 | }); 17 | 18 | const ssm = new AWS.SSM({ 19 | region: 'us-east-1' 20 | }); 21 | 22 | 23 | exports.handler = function(event, context, callback) { 24 | readDragons(event,callback); 25 | } 26 | 27 | async function readDragons(event, callback) { 28 | 29 | var fileName = await getFileName(); 30 | var bucketName = await getBucketName(); 31 | var dragonData = readDragonsFromS3(bucketName, fileName, event, callback); 32 | 33 | } 34 | 35 | async function getFileName() { 36 | 37 | var fileNameParams = { 38 | Name: 'dragon_data_file_name', 39 | WithDecryption: false 40 | }; 41 | var promise = await ssm.getParameter(fileNameParams).promise(); 42 | return promise.Parameter.Value; 43 | } 44 | 45 | async function getBucketName() { 46 | var bucketNameParams = { 47 | Name: 'dragon_data_bucket_name', 48 | WithDecryption: false 49 | }; 50 | 51 | var promise = await ssm.getParameter(bucketNameParams).promise(); 52 | return promise.Parameter.Value; 53 | 54 | } 55 | 56 | function readDragonsFromS3(bucketName, fileName, event, callback) { 57 | 58 | var expression = "select * from s3object s" 59 | 60 | if(event.queryStringParameters && event.queryStringParameters.dragonName && event.queryStringParameters.dragonName !== "") 61 | expression = "select * from S3Object[*][*] s where s.dragon_name_str = '" + event["queryStringParameters"]['dragonName'] + "'" 62 | else if (event.queryStringParameters && event.queryStringParameters.family && event.queryStringParameters.family !== "") 63 | expression = "select * from S3Object[*][*] s where s.family_str = '" + event["queryStringParameters"]['family'] + "'" 64 | 65 | s3.selectObjectContent({ 66 | Bucket: bucketName, 67 | Expression: expression, 68 | ExpressionType: 'SQL', 69 | Key: fileName, 70 | InputSerialization: { 71 | JSON: { 72 | Type: 'DOCUMENT', 73 | } 74 | }, 75 | OutputSerialization: { 76 | JSON: { 77 | RecordDelimiter: ',' 78 | } 79 | } 80 | }, function(err, data) { 81 | if (err) { 82 | console.log(err); 83 | } else { 84 | return handleData(data, callback); 85 | } 86 | } 87 | ); 88 | } 89 | 90 | function handleData(data, callback) { 91 | var event = data.Payload; 92 | var dataToReturn; 93 | 94 | event.on('data', function(event) { 95 | if (event.Records) { 96 | if(event.Records){ 97 | dataToReturn += event.Records.Payload.toString(); 98 | } 99 | } 100 | }); 101 | 102 | event.on("end", function() { 103 | callback(null, { 104 | "statusCode":200, 105 | "body":JSON.stringify(dataToReturn)}) 106 | }); 107 | 108 | } -------------------------------------------------------------------------------- /API Gateway Demo/ResponseMappingForMock.vtl: -------------------------------------------------------------------------------- 1 | [ 2 | #if( $input.params('family') == "red" ) 3 | { 4 | "description_str":"Xanya is the fire tribe's banished general. She broke ranks and has been wandering ever since.", 5 | "dragon_name_str":"Xanya", 6 | "family_str":"red", 7 | "location_city_str":"las vegas", 8 | "location_country_str":"usa", 9 | "location_neighborhood_str":"e clark ave", 10 | "location_state_str":"nevada" 11 | }, { 12 | "description_str":"Eislex flies with the fire sprites. He protects them and is their guardian.", 13 | "dragon_name_str":"Eislex", 14 | "family_str":"red", 15 | "location_city_str":"st. cloud", 16 | "location_country_str":"usa", 17 | "location_neighborhood_str":"breckenridge ave", 18 | "location_state_str":"minnesota" } 19 | #elseif( $input.params('family') == "blue" ) 20 | { 21 | "description_str":"Protheus is a wise and ancient dragon that serves on the grand council in the sky world. He uses his power to calm those near him.", 22 | "dragon_name_str":"Protheus", 23 | "family_str":"blue", 24 | "location_city_str":"brandon", 25 | "location_country_str":"usa", 26 | "location_neighborhood_str":"e morgan st", 27 | "location_state_str":"florida" 28 | } 29 | #elseif( $input.params('dragonName') == "Atlas" ) 30 | { 31 | "description_str":"From the northern fire tribe, Atlas was born from the ashes of his fallen father in combat. He is fearless and does not fear battle.", 32 | "dragon_name_str":"Atlas", 33 | "family_str":"red", 34 | "location_city_str":"anchorage", 35 | "location_country_str":"usa", 36 | "location_neighborhood_str":"w fireweed ln", 37 | "location_state_str":"alaska" 38 | } 39 | #else 40 | { 41 | "description_str":"From the northern fire tribe, Atlas was born from the ashes of his fallen father in combat. He is fearless and does not fear battle.", 42 | "dragon_name_str":"Atlas", 43 | "family_str":"red", 44 | "location_city_str":"anchorage", 45 | "location_country_str":"usa", 46 | "location_neighborhood_str":"w fireweed ln", 47 | "location_state_str":"alaska" 48 | }, 49 | { 50 | "description_str":"Protheus is a wise and ancient dragon that serves on the grand council in the sky world. He uses his power to calm those near him.", 51 | "dragon_name_str":"Protheus", 52 | "family_str":"blue", 53 | "location_city_str":"brandon", 54 | "location_country_str":"usa", 55 | "location_neighborhood_str":"e morgan st", 56 | "location_state_str":"florida" 57 | }, 58 | { 59 | "description_str":"Xanya is the fire tribe's banished general. She broke ranks and has been wandering ever since.", 60 | "dragon_name_str":"Xanya", 61 | "family_str":"red", 62 | "location_city_str":"las vegas", 63 | "location_country_str":"usa", 64 | "location_neighborhood_str":"e clark ave", 65 | "location_state_str":"nevada" 66 | }, 67 | { 68 | "description_str":"Eislex flies with the fire sprites. He protects them and is their guardian.", 69 | "dragon_name_str":"Eislex", 70 | "family_str":"red", 71 | "location_city_str":"st. cloud", 72 | "location_country_str":"usa", 73 | "location_neighborhood_str":"breckenridge ave", 74 | "location_state_str":"minnesota" 75 | } 76 | #end 77 | ] 78 | 79 | -------------------------------------------------------------------------------- /Lambda Code/Node/examples/add-dragon/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "requires": true, 3 | "lockfileVersion": 1, 4 | "dependencies": { 5 | "aws-sdk": { 6 | "version": "2.682.0", 7 | "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.682.0.tgz", 8 | "integrity": "sha512-1eVkM/L53Vi68h07EbPh/1MqL0xI7sKssJAcFACLsBISJTqogxOrIocqPlVoQoprUFK4KiSL5ucK88KwkyoCUA==", 9 | "requires": { 10 | "buffer": "4.9.1", 11 | "events": "1.1.1", 12 | "ieee754": "1.1.13", 13 | "jmespath": "0.15.0", 14 | "querystring": "0.2.0", 15 | "sax": "1.2.1", 16 | "url": "0.10.3", 17 | "uuid": "3.3.2", 18 | "xml2js": "0.4.19" 19 | } 20 | }, 21 | "base64-js": { 22 | "version": "1.3.1", 23 | "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", 24 | "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" 25 | }, 26 | "buffer": { 27 | "version": "4.9.1", 28 | "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", 29 | "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", 30 | "requires": { 31 | "base64-js": "^1.0.2", 32 | "ieee754": "^1.1.4", 33 | "isarray": "^1.0.0" 34 | } 35 | }, 36 | "events": { 37 | "version": "1.1.1", 38 | "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", 39 | "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" 40 | }, 41 | "ieee754": { 42 | "version": "1.1.13", 43 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", 44 | "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" 45 | }, 46 | "isarray": { 47 | "version": "1.0.0", 48 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 49 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" 50 | }, 51 | "jmespath": { 52 | "version": "0.15.0", 53 | "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", 54 | "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" 55 | }, 56 | "punycode": { 57 | "version": "1.3.2", 58 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", 59 | "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" 60 | }, 61 | "querystring": { 62 | "version": "0.2.0", 63 | "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", 64 | "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" 65 | }, 66 | "sax": { 67 | "version": "1.2.1", 68 | "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", 69 | "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" 70 | }, 71 | "url": { 72 | "version": "0.10.3", 73 | "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", 74 | "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", 75 | "requires": { 76 | "punycode": "1.3.2", 77 | "querystring": "0.2.0" 78 | } 79 | }, 80 | "uuid": { 81 | "version": "3.3.2", 82 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", 83 | "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" 84 | }, 85 | "xml2js": { 86 | "version": "0.4.19", 87 | "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", 88 | "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", 89 | "requires": { 90 | "sax": ">=0.6.0", 91 | "xmlbuilder": "~9.0.1" 92 | } 93 | }, 94 | "xmlbuilder": { 95 | "version": "9.0.7", 96 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", 97 | "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Lambda Code/Node/examples/validate-dragon/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "requires": true, 3 | "lockfileVersion": 1, 4 | "dependencies": { 5 | "aws-sdk": { 6 | "version": "2.682.0", 7 | "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.682.0.tgz", 8 | "integrity": "sha512-1eVkM/L53Vi68h07EbPh/1MqL0xI7sKssJAcFACLsBISJTqogxOrIocqPlVoQoprUFK4KiSL5ucK88KwkyoCUA==", 9 | "requires": { 10 | "buffer": "4.9.1", 11 | "events": "1.1.1", 12 | "ieee754": "1.1.13", 13 | "jmespath": "0.15.0", 14 | "querystring": "0.2.0", 15 | "sax": "1.2.1", 16 | "url": "0.10.3", 17 | "uuid": "3.3.2", 18 | "xml2js": "0.4.19" 19 | } 20 | }, 21 | "base64-js": { 22 | "version": "1.3.1", 23 | "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", 24 | "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" 25 | }, 26 | "buffer": { 27 | "version": "4.9.1", 28 | "resolved": "https://registry.npmjs.org/buffer/-/buffer-4.9.1.tgz", 29 | "integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=", 30 | "requires": { 31 | "base64-js": "^1.0.2", 32 | "ieee754": "^1.1.4", 33 | "isarray": "^1.0.0" 34 | } 35 | }, 36 | "events": { 37 | "version": "1.1.1", 38 | "resolved": "https://registry.npmjs.org/events/-/events-1.1.1.tgz", 39 | "integrity": "sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ=" 40 | }, 41 | "ieee754": { 42 | "version": "1.1.13", 43 | "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", 44 | "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" 45 | }, 46 | "isarray": { 47 | "version": "1.0.0", 48 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 49 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" 50 | }, 51 | "jmespath": { 52 | "version": "0.15.0", 53 | "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", 54 | "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" 55 | }, 56 | "punycode": { 57 | "version": "1.3.2", 58 | "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", 59 | "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" 60 | }, 61 | "querystring": { 62 | "version": "0.2.0", 63 | "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", 64 | "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" 65 | }, 66 | "sax": { 67 | "version": "1.2.1", 68 | "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", 69 | "integrity": "sha1-e45lYZCyKOgaZq6nSEgNgozS03o=" 70 | }, 71 | "url": { 72 | "version": "0.10.3", 73 | "resolved": "https://registry.npmjs.org/url/-/url-0.10.3.tgz", 74 | "integrity": "sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ=", 75 | "requires": { 76 | "punycode": "1.3.2", 77 | "querystring": "0.2.0" 78 | } 79 | }, 80 | "uuid": { 81 | "version": "3.3.2", 82 | "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", 83 | "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" 84 | }, 85 | "xml2js": { 86 | "version": "0.4.19", 87 | "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz", 88 | "integrity": "sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q==", 89 | "requires": { 90 | "sax": ">=0.6.0", 91 | "xmlbuilder": "~9.0.1" 92 | } 93 | }, 94 | "xmlbuilder": { 95 | "version": "9.0.7", 96 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", 97 | "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/addDragon/add-dragon/src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | import com.mycompany.app.model.Dragon; 15 | import com.amazonaws.services.s3.AmazonS3; 16 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 17 | import com.amazonaws.services.s3.model.S3Object; 18 | import com.amazonaws.services.s3.model.GetObjectRequest; 19 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; 20 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; 21 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; 22 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; 23 | import java.util.*; 24 | import java.io.InputStream; 25 | import java.io.BufferedReader; 26 | import java.io.InputStreamReader; 27 | import java.io.IOException; 28 | import com.amazonaws.services.lambda.runtime.Context; 29 | import com.amazonaws.services.lambda.runtime.RequestHandler; 30 | import com.google.gson.reflect.TypeToken; 31 | import com.google.gson.Gson; 32 | import com.google.gson.GsonBuilder; 33 | 34 | public class App implements RequestHandler { 35 | 36 | private static final AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); 37 | private static final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); 38 | private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); 39 | 40 | public String handleRequest(Dragon event, Context context) { 41 | addDragon(event); 42 | return "Dragon added"; 43 | } 44 | 45 | private static void addDragon(Dragon event) { 46 | // get object from S3 47 | S3Object object = s3Client.getObject(new GetObjectRequest(getBucketName(), getKey())); 48 | // get object contents as input stream 49 | InputStream objectInputStream = object.getObjectContent(); 50 | // convert input stream to string 51 | String dragonDataString = convertTextInputStreamToString(objectInputStream); 52 | // convert string to List to work with more easily 53 | // This is because I am trying to avoid doing raw string manipulation 54 | List dragonDataList = convertStringtoList(dragonDataString); 55 | // add dragon to List 56 | addNewDragonToList(event, dragonDataList); 57 | uploadObjectToS3(getBucketName(), getKey(), dragonDataList); 58 | } 59 | 60 | private static String getBucketName() { 61 | GetParameterRequest bucketParameterRequest = new GetParameterRequest().withName("dragon_data_bucket_name").withWithDecryption(false); 62 | GetParameterResult bucketResult = ssm.getParameter(bucketParameterRequest); 63 | return bucketResult.getParameter().getValue(); 64 | } 65 | 66 | private static String getKey() { 67 | GetParameterRequest keyParameterRequest = new GetParameterRequest().withName("dragon_data_file_name").withWithDecryption(false); 68 | GetParameterResult keyResult = ssm.getParameter(keyParameterRequest); 69 | return keyResult.getParameter().getValue(); 70 | } 71 | 72 | private static String convertTextInputStreamToString(InputStream input) { 73 | // Read the text input stream one line at a time and display each line. 74 | BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 75 | String line = null; 76 | String objectContent = ""; 77 | try { 78 | while ((line = reader.readLine()) != null) { 79 | objectContent += line; 80 | } 81 | } catch (IOException e) { 82 | // in the real world please do something with errors 83 | // do not just log them 84 | // do as I say not as I do 85 | System.out.println(e.getMessage()); 86 | } 87 | return objectContent; 88 | } 89 | 90 | private static List convertStringtoList(String dragonData) { 91 | return gson.fromJson(dragonData, new TypeToken>(){}.getType()); 92 | } 93 | 94 | private static void addNewDragonToList(Dragon newDragon, List dragons) { 95 | dragons.add(newDragon); 96 | } 97 | 98 | private static void uploadObjectToS3(String bucketName, String key, List dragons) { 99 | // uploads the object to S3 100 | // converts List to a JSON String before writing 101 | s3Client.putObject(bucketName, key, gson.toJson(dragons)); 102 | } 103 | 104 | } -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/validateDragon/validate-dragon/src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | import com.amazonaws.services.s3.AmazonS3; 15 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 16 | import com.amazonaws.services.s3.model.JSONInput; 17 | import com.amazonaws.services.s3.model.JSONOutput; 18 | import com.amazonaws.services.s3.model.CompressionType; 19 | import com.amazonaws.services.s3.model.ExpressionType; 20 | import com.amazonaws.services.s3.model.InputSerialization; 21 | import com.amazonaws.services.s3.model.OutputSerialization; 22 | import com.amazonaws.services.s3.model.SelectObjectContentEvent; 23 | import com.amazonaws.services.s3.model.SelectObjectContentEventVisitor; 24 | import com.amazonaws.services.s3.model.SelectObjectContentRequest; 25 | import com.amazonaws.services.s3.model.SelectObjectContentResult; 26 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; 27 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; 28 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; 29 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; 30 | import java.util.*; 31 | import java.io.InputStream; 32 | import java.util.concurrent.atomic.AtomicBoolean; 33 | import java.io.IOException; 34 | import org.apache.commons.io.IOUtils; 35 | import java.nio.charset.StandardCharsets; 36 | import com.amazonaws.services.lambda.runtime.Context; 37 | import com.amazonaws.services.lambda.runtime.RequestHandler; 38 | 39 | public class App implements RequestHandler, String> { 40 | 41 | private static final AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); 42 | private static final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); 43 | 44 | @Override 45 | public String handleRequest(Map event, Context context) { 46 | readDragonData(event); 47 | return "Dragon validated"; 48 | } 49 | 50 | protected static void readDragonData(Map event) { 51 | String bucketName = getBucketName(); 52 | String key = getKey(); 53 | String query = getQuery(event); 54 | SelectObjectContentRequest request = generateJSONRequest(bucketName, key, query); 55 | final AtomicBoolean isResultComplete = new AtomicBoolean(false); 56 | 57 | SelectObjectContentResult result = s3Client.selectObjectContent(request); 58 | 59 | // Anonymous inner class implementation 60 | // to define what actions to complete 61 | // for every object in the result stream 62 | InputStream resultInputStream = result.getPayload().getRecordsInputStream( 63 | new SelectObjectContentEventVisitor() { 64 | @Override 65 | public void visit(SelectObjectContentEvent.StatsEvent event) 66 | { 67 | System.out.println( 68 | "Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() 69 | + " Bytes Processed: " + event.getDetails().getBytesProcessed()); 70 | } 71 | 72 | /* 73 | * An End Event informs that the request has finished successfully. 74 | */ 75 | @Override 76 | public void visit(SelectObjectContentEvent.EndEvent event) 77 | { 78 | isResultComplete.set(true); 79 | } 80 | } 81 | ); 82 | 83 | String text = null; 84 | try { 85 | text = IOUtils.toString(resultInputStream, StandardCharsets.UTF_8.name()); 86 | } catch (IOException e) { 87 | // In the real world, you should do actual error handling here 88 | // do not just log a message and move on in a production system 89 | System.out.println(e.getMessage()); 90 | } 91 | if(text != null && !(text.equals(""))) { 92 | throw new DragonValidationException("Duplicate dragon reported", new RuntimeException()); 93 | } 94 | 95 | } 96 | 97 | 98 | private static SelectObjectContentRequest generateJSONRequest(String bucketName, String key, String query) { 99 | SelectObjectContentRequest request = new SelectObjectContentRequest(); 100 | request.setBucketName(bucketName); 101 | request.setKey(key); 102 | request.setExpression(query); 103 | request.setExpressionType(ExpressionType.SQL); 104 | 105 | InputSerialization inputSerialization = new InputSerialization(); 106 | inputSerialization.setJson(new JSONInput().withType("Document")); 107 | inputSerialization.setCompressionType(CompressionType.NONE); 108 | request.setInputSerialization(inputSerialization); 109 | 110 | OutputSerialization outputSerialization = new OutputSerialization(); 111 | outputSerialization.setJson(new JSONOutput()); 112 | request.setOutputSerialization(outputSerialization); 113 | 114 | return request; 115 | } 116 | 117 | private static String getBucketName() { 118 | GetParameterRequest bucketParameterRequest = new GetParameterRequest().withName("dragon_data_bucket_name").withWithDecryption(false); 119 | GetParameterResult bucketResult = ssm.getParameter(bucketParameterRequest); 120 | return bucketResult.getParameter().getValue(); 121 | } 122 | 123 | private static String getKey() { 124 | GetParameterRequest keyParameterRequest = new GetParameterRequest().withName("dragon_data_file_name").withWithDecryption(false); 125 | GetParameterResult keyResult = ssm.getParameter(keyParameterRequest); 126 | return keyResult.getParameter().getValue(); 127 | } 128 | 129 | private static String getQuery(Map event) { 130 | return "select * from S3Object[*][*] s where s.dragon_name_str = '" + event.get("dragon_name_str") + "'"; 131 | } 132 | } -------------------------------------------------------------------------------- /Lambda Code/Java/sdkExploration/my-app/src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | import com.amazonaws.services.s3.AmazonS3; 15 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 16 | import com.amazonaws.services.s3.model.JSONInput; 17 | import com.amazonaws.services.s3.model.JSONOutput; 18 | import com.amazonaws.services.s3.model.CompressionType; 19 | import com.amazonaws.services.s3.model.ExpressionType; 20 | import com.amazonaws.services.s3.model.InputSerialization; 21 | import com.amazonaws.services.s3.model.OutputSerialization; 22 | import com.amazonaws.services.s3.model.SelectObjectContentEvent; 23 | import com.amazonaws.services.s3.model.SelectObjectContentEventVisitor; 24 | import com.amazonaws.services.s3.model.SelectObjectContentRequest; 25 | import com.amazonaws.services.s3.model.SelectObjectContentResult; 26 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; 27 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; 28 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; 29 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; 30 | import java.io.InputStream; 31 | import java.util.concurrent.atomic.AtomicBoolean; 32 | import java.io.IOException; 33 | import org.apache.commons.io.IOUtils; 34 | import java.nio.charset.StandardCharsets; 35 | 36 | public class App { 37 | 38 | private static final AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); 39 | private static final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); 40 | 41 | public static void main(String[] args) throws Exception { 42 | readDragonData(); 43 | } 44 | 45 | private static void readDragonData() { 46 | String bucketName = getBucketName(); 47 | String key = getKey(); 48 | String query = getQuery(); 49 | SelectObjectContentRequest request = generateJSONRequest(bucketName, key, query); 50 | String dragonData = queryS3(request); 51 | System.out.println(dragonData); 52 | } 53 | 54 | private static String queryS3(SelectObjectContentRequest request) { 55 | final AtomicBoolean isResultComplete = new AtomicBoolean(false); 56 | 57 | // Call S3 select 58 | SelectObjectContentResult result = s3Client.selectObjectContent(request); 59 | 60 | // Anonymous inner class implementation 61 | // to define what actions to complete 62 | // for every object in the result stream 63 | InputStream resultInputStream = result.getPayload().getRecordsInputStream( 64 | new SelectObjectContentEventVisitor() { 65 | @Override 66 | public void visit(SelectObjectContentEvent.StatsEvent event) 67 | { 68 | System.out.println( 69 | "Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() 70 | + " Bytes Processed: " + event.getDetails().getBytesProcessed()); 71 | } 72 | 73 | /* 74 | * An End Event informs that the request has finished successfully. 75 | */ 76 | @Override 77 | public void visit(SelectObjectContentEvent.EndEvent event) 78 | { 79 | isResultComplete.set(true); 80 | System.out.println("Received End Event. Result is complete."); 81 | } 82 | } 83 | ); 84 | 85 | String text = null; 86 | try { 87 | text = IOUtils.toString(resultInputStream, StandardCharsets.UTF_8.name()); 88 | } catch (IOException e) { 89 | // In the real world, you should do actual error handling here 90 | // do not just log a message and move on in a production system 91 | System.out.println(e.getMessage()); 92 | } 93 | return text; 94 | } 95 | 96 | private static SelectObjectContentRequest generateJSONRequest(String bucketName, String key, String query) { 97 | SelectObjectContentRequest request = new SelectObjectContentRequest(); 98 | request.setBucketName(bucketName); 99 | request.setKey(key); 100 | request.setExpression(query); 101 | request.setExpressionType(ExpressionType.SQL); 102 | 103 | InputSerialization inputSerialization = new InputSerialization(); 104 | inputSerialization.setJson(new JSONInput().withType("Document")); 105 | inputSerialization.setCompressionType(CompressionType.NONE); 106 | request.setInputSerialization(inputSerialization); 107 | 108 | OutputSerialization outputSerialization = new OutputSerialization(); 109 | outputSerialization.setJson(new JSONOutput()); 110 | request.setOutputSerialization(outputSerialization); 111 | 112 | return request; 113 | } 114 | 115 | private static String getBucketName() { 116 | GetParameterRequest bucketParameterRequest = new GetParameterRequest().withName("dragon_data_bucket_name").withWithDecryption(false); 117 | GetParameterResult bucketResult = ssm.getParameter(bucketParameterRequest); 118 | return bucketResult.getParameter().getValue(); 119 | } 120 | 121 | private static String getKey() { 122 | GetParameterRequest keyParameterRequest = new GetParameterRequest().withName("dragon_data_file_name").withWithDecryption(false); 123 | GetParameterResult keyResult = ssm.getParameter(keyParameterRequest); 124 | return keyResult.getParameter().getValue(); 125 | } 126 | 127 | private static String getQuery() { 128 | // later on this method will return different results based 129 | // on query string parameters. For now, we will hardcode the results 130 | // to select *, which isn't the best showcase of S3 select 131 | // but don't worry we will get there 132 | return "select * from s3object s"; 133 | } 134 | } -------------------------------------------------------------------------------- /Lambda Code/Java/lambda-demo/listDragons/my-app/src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | import com.amazonaws.services.s3.AmazonS3; 15 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 16 | import com.amazonaws.services.s3.model.JSONInput; 17 | import com.amazonaws.services.s3.model.JSONOutput; 18 | import com.amazonaws.services.s3.model.CompressionType; 19 | import com.amazonaws.services.s3.model.ExpressionType; 20 | import com.amazonaws.services.s3.model.InputSerialization; 21 | import com.amazonaws.services.s3.model.OutputSerialization; 22 | import com.amazonaws.services.s3.model.SelectObjectContentEvent; 23 | import com.amazonaws.services.s3.model.SelectObjectContentEventVisitor; 24 | import com.amazonaws.services.s3.model.SelectObjectContentRequest; 25 | import com.amazonaws.services.s3.model.SelectObjectContentResult; 26 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; 27 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; 28 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; 29 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; 30 | import java.util.*; 31 | import java.io.InputStream; 32 | import java.util.concurrent.atomic.AtomicBoolean; 33 | import java.io.IOException; 34 | import org.apache.commons.io.IOUtils; 35 | import java.nio.charset.StandardCharsets; 36 | import com.google.gson.reflect.TypeToken; 37 | import com.google.gson.Gson; 38 | import com.google.gson.GsonBuilder; 39 | import com.amazonaws.services.lambda.runtime.Context; 40 | import com.amazonaws.services.lambda.runtime.RequestHandler; 41 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; 42 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; 43 | 44 | 45 | public class App implements RequestHandler { 46 | 47 | private static final AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); 48 | private static final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); 49 | 50 | @Override 51 | public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) { 52 | String dragonData = readDragonData(event); 53 | return generateResponse(dragonData); 54 | } 55 | 56 | protected static String readDragonData(APIGatewayProxyRequestEvent event) { 57 | Map queryParams = event.getQueryStringParameters(); 58 | String bucketName = getBucketName(); 59 | String key = getKey(); 60 | String query = getQuery(queryParams); 61 | SelectObjectContentRequest request = generateJSONRequest(bucketName, key, query); 62 | return queryS3(request); 63 | } 64 | 65 | private static String queryS3(SelectObjectContentRequest request) { 66 | final AtomicBoolean isResultComplete = new AtomicBoolean(false); 67 | // Call S3 Select 68 | SelectObjectContentResult result = s3Client.selectObjectContent(request); 69 | 70 | // Anonymous inner class implementation 71 | // to define what actions to complete 72 | // for every object in the result stream 73 | InputStream resultInputStream = result.getPayload().getRecordsInputStream( 74 | new SelectObjectContentEventVisitor() { 75 | @Override 76 | public void visit(SelectObjectContentEvent.StatsEvent event) 77 | { 78 | System.out.println( 79 | "Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() 80 | + " Bytes Processed: " + event.getDetails().getBytesProcessed()); 81 | } 82 | 83 | /* 84 | * An End Event informs that the request has finished successfully. 85 | */ 86 | @Override 87 | public void visit(SelectObjectContentEvent.EndEvent event) 88 | { 89 | isResultComplete.set(true); 90 | System.out.println("Received End Event. Result is complete."); 91 | } 92 | } 93 | ); 94 | 95 | String text = null; 96 | try { 97 | text = IOUtils.toString(resultInputStream, StandardCharsets.UTF_8.name()); 98 | } catch (IOException e) { 99 | // In the real world, you should do actual error handling here 100 | // do not just log a message and move on in a production system 101 | System.out.println(e.getMessage()); 102 | } 103 | return text; 104 | } 105 | 106 | private static SelectObjectContentRequest generateJSONRequest(String bucketName, String key, String query) { 107 | SelectObjectContentRequest request = new SelectObjectContentRequest(); 108 | request.setBucketName(bucketName); 109 | request.setKey(key); 110 | request.setExpression(query); 111 | request.setExpressionType(ExpressionType.SQL); 112 | 113 | InputSerialization inputSerialization = new InputSerialization(); 114 | inputSerialization.setJson(new JSONInput().withType("Document")); 115 | inputSerialization.setCompressionType(CompressionType.NONE); 116 | request.setInputSerialization(inputSerialization); 117 | 118 | OutputSerialization outputSerialization = new OutputSerialization(); 119 | outputSerialization.setJson(new JSONOutput()); 120 | request.setOutputSerialization(outputSerialization); 121 | 122 | return request; 123 | } 124 | 125 | private static String getBucketName() { 126 | GetParameterRequest bucketParameterRequest = new GetParameterRequest().withName("dragon_data_bucket_name").withWithDecryption(false); 127 | GetParameterResult bucketResult = ssm.getParameter(bucketParameterRequest); 128 | return bucketResult.getParameter().getValue(); 129 | } 130 | 131 | private static String getKey() { 132 | GetParameterRequest keyParameterRequest = new GetParameterRequest().withName("dragon_data_file_name").withWithDecryption(false); 133 | GetParameterResult keyResult = ssm.getParameter(keyParameterRequest); 134 | return keyResult.getParameter().getValue(); 135 | } 136 | 137 | private static String getQuery(Map queryParams) { 138 | if(queryParams != null) { 139 | System.out.println("we have params"); 140 | if (queryParams.containsKey("family")) { 141 | System.out.println(queryParams.get("family")); 142 | return "select * from S3Object[*][*] s where s.family_str = '" + queryParams.get("family") + "'"; 143 | 144 | } else if (queryParams.containsKey("dragonName")) { 145 | return "select * from S3Object[*][*] s where s.dragon_name_str = '" + queryParams.get("dragonName") + "'"; 146 | } 147 | } 148 | 149 | return "select * from s3object s"; 150 | } 151 | 152 | private static APIGatewayProxyResponseEvent generateResponse(String dragons) { 153 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); 154 | APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent(); 155 | response.setStatusCode(200); 156 | response.setBody(gson.toJson(dragons)); 157 | return response; 158 | } 159 | } -------------------------------------------------------------------------------- /Lambda Code/Java/xray-demo/my-app/src/main/java/com/mycompany/app/App.java: -------------------------------------------------------------------------------- 1 | // # Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // # 3 | // # Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except 4 | // # in compliance with the License. A copy of the License is located at 5 | // # 6 | // # https://aws.amazon.com/apache-2-0/ 7 | // # 8 | // # or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, 9 | // # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 10 | // # specific language governing permissions and limitations under the License. 11 | 12 | package com.mycompany.app; 13 | 14 | import com.amazonaws.services.s3.AmazonS3; 15 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; 16 | import com.amazonaws.services.s3.model.JSONInput; 17 | import com.amazonaws.services.s3.model.JSONOutput; 18 | import com.amazonaws.services.s3.model.CompressionType; 19 | import com.amazonaws.services.s3.model.ExpressionType; 20 | import com.amazonaws.services.s3.model.InputSerialization; 21 | import com.amazonaws.services.s3.model.OutputSerialization; 22 | import com.amazonaws.services.s3.model.SelectObjectContentEvent; 23 | import com.amazonaws.services.s3.model.SelectObjectContentEventVisitor; 24 | import com.amazonaws.services.s3.model.SelectObjectContentRequest; 25 | import com.amazonaws.services.s3.model.SelectObjectContentResult; 26 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; 27 | import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; 28 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; 29 | import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; 30 | import java.util.*; 31 | import java.io.InputStream; 32 | import java.util.concurrent.atomic.AtomicBoolean; 33 | import java.io.IOException; 34 | import org.apache.commons.io.IOUtils; 35 | import java.nio.charset.StandardCharsets; 36 | import com.amazonaws.services.lambda.runtime.Context; 37 | import com.amazonaws.services.lambda.runtime.RequestHandler; 38 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; 39 | import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; 40 | import com.google.gson.reflect.TypeToken; 41 | import com.google.gson.Gson; 42 | import com.google.gson.GsonBuilder; 43 | import com.amazonaws.xray.AWSXRay; 44 | import com.amazonaws.xray.entities.Subsegment; 45 | 46 | public class App implements RequestHandler { 47 | 48 | private static final AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.defaultClient(); 49 | private static final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); 50 | 51 | @Override 52 | public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) { 53 | String dragonData = readDragonData(event); 54 | return generateResponse(dragonData); 55 | } 56 | 57 | protected static String readDragonData(APIGatewayProxyRequestEvent event) { 58 | Map queryParams = event.getQueryStringParameters(); 59 | String bucketName = getBucketName(); 60 | String key = getKey(); 61 | String query = getQuery(queryParams); 62 | SelectObjectContentRequest request = generateJSONRequest(bucketName, key, query); 63 | return queryS3(request); 64 | } 65 | 66 | private static String queryS3(SelectObjectContentRequest request) { 67 | final AtomicBoolean isResultComplete = new AtomicBoolean(false); 68 | 69 | Subsegment subsegment = AWSXRay.beginSubsegment("S3 Select Query"); 70 | String text = ""; 71 | try { 72 | SelectObjectContentResult result = s3Client.selectObjectContent(request); 73 | 74 | // Anonymous inner class implementation 75 | // to define what actions to complete 76 | // for every object in the result stream 77 | InputStream resultInputStream = result.getPayload().getRecordsInputStream( 78 | new SelectObjectContentEventVisitor() { 79 | @Override 80 | public void visit(SelectObjectContentEvent.StatsEvent event) 81 | { 82 | System.out.println( 83 | "Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() 84 | + " Bytes Processed: " + event.getDetails().getBytesProcessed()); 85 | } 86 | 87 | /* 88 | * An End Event informs that the request has finished successfully. 89 | */ 90 | @Override 91 | public void visit(SelectObjectContentEvent.EndEvent event) 92 | { 93 | isResultComplete.set(true); 94 | System.out.println("Received End Event. Result is complete."); 95 | } 96 | } 97 | ); 98 | 99 | 100 | try { 101 | text = IOUtils.toString(resultInputStream, StandardCharsets.UTF_8.name()); 102 | } catch (IOException e) { 103 | // In the real world, you should do actual error handling here 104 | // do not just log a message and move on in a production system 105 | System.out.println(e.getMessage()); 106 | } 107 | } catch (Exception e) { 108 | subsegment.addException(e); 109 | } finally { 110 | AWSXRay.endSubsegment(); 111 | } 112 | return text; 113 | } 114 | 115 | private static SelectObjectContentRequest generateJSONRequest(String bucketName, String key, String query) { 116 | SelectObjectContentRequest request = new SelectObjectContentRequest(); 117 | request.setBucketName(bucketName); 118 | request.setKey(key); 119 | request.setExpression(query); 120 | request.setExpressionType(ExpressionType.SQL); 121 | 122 | InputSerialization inputSerialization = new InputSerialization(); 123 | inputSerialization.setJson(new JSONInput().withType("Document")); 124 | inputSerialization.setCompressionType(CompressionType.NONE); 125 | request.setInputSerialization(inputSerialization); 126 | 127 | OutputSerialization outputSerialization = new OutputSerialization(); 128 | outputSerialization.setJson(new JSONOutput()); 129 | request.setOutputSerialization(outputSerialization); 130 | 131 | return request; 132 | } 133 | 134 | private static String getBucketName() { 135 | GetParameterRequest bucketParameterRequest = new GetParameterRequest().withName("dragon_data_bucket_name").withWithDecryption(false); 136 | GetParameterResult bucketResult = ssm.getParameter(bucketParameterRequest); 137 | return bucketResult.getParameter().getValue(); 138 | } 139 | 140 | private static String getKey() { 141 | GetParameterRequest keyParameterRequest = new GetParameterRequest().withName("dragon_data_file_name").withWithDecryption(false); 142 | GetParameterResult keyResult = ssm.getParameter(keyParameterRequest); 143 | return keyResult.getParameter().getValue(); 144 | } 145 | 146 | private static String getQuery(Map queryParams) { 147 | if(queryParams != null) { 148 | if (queryParams.containsKey("family")) { 149 | return "select * from S3Object[*][*] s where s.family_str = '" + queryParams.get("family") + "'"; 150 | 151 | } else if (queryParams.containsKey("dragonName")) { 152 | return "select * from S3Object[*][*] s where s.dragon_name_str = '" + queryParams.get("dragonName") + "'"; 153 | } 154 | } 155 | 156 | return "select * from s3object s"; 157 | } 158 | 159 | private static APIGatewayProxyResponseEvent generateResponse(String dragons) { 160 | APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent(); 161 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); 162 | response.setStatusCode(200); 163 | response.setBody(gson.toJson(dragons)); 164 | return response; 165 | } 166 | } -------------------------------------------------------------------------------- /Dragon Data/dragon_stats_one.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "description_str": "From the northern fire tribe, Atlas was born from the ashes of his fallen father in combat. He is fearless and does not fear battle.", 4 | "dragon_name_str": "Atlas", 5 | "family_str": "red", 6 | "location_city_str": "anchorage", 7 | "location_country_str": "usa", 8 | "location_neighborhood_str": "w fireweed ln", 9 | "location_state_str": "alaska" 10 | }, 11 | { 12 | "description_str": "Bahamethut is an immortal dragon. Cruel and ruthless, he comes from the undead realm and is a judge among his dragon tribe. ", 13 | "dragon_name_str": "Bahamethut", 14 | "family_str": "black", 15 | "location_city_str": "las vegas", 16 | "location_country_str": "usa", 17 | "location_neighborhood_str": "shore breeze dr", 18 | "location_state_str": "nevada" 19 | }, 20 | { 21 | "description_str": "Frealu has an ice breath that can freeze her enemies into a paralyzed state. She is from the souther water tribe.", 22 | "dragon_name_str": "Frealu", 23 | "family_str": "blue", 24 | "location_city_str": "mesa", 25 | "location_country_str": "usa", 26 | "location_neighborhood_str": "e adobe st", 27 | "location_state_str": "arizona" 28 | }, 29 | { 30 | "description_str": "Pradumo is a sage from the easter woods. She practices healing medicine.", 31 | "dragon_name_str": "Pradumo", 32 | "family_str": "green", 33 | "location_city_str": "sterling", 34 | "location_country_str": "usa", 35 | "location_neighborhood_str": "broadway", 36 | "location_state_str": "colorado" 37 | }, 38 | { 39 | "description_str": "Tagnaurak's breathe melts and refines precious gems. He covets money and loves shiny things that he obsessively collects.", 40 | "dragon_name_str": "Tagnaurak", 41 | "family_str": "red", 42 | "location_city_str": "las vegas", 43 | "location_country_str": "usa", 44 | "location_neighborhood_str": "westcliff dr", 45 | "location_state_str": "nevada" 46 | }, 47 | { 48 | "description_str": "Midnight has a stealth attack mode. He can disappear and ghost for sneak attacks.", 49 | "dragon_name_str": "Midnight", 50 | "family_str": "black", 51 | "location_city_str": "cupertino", 52 | "location_country_str": "usa", 53 | "location_neighborhood_str": "pear tree ln", 54 | "location_state_str": "california" 55 | }, 56 | { 57 | "description_str": "Longlu is a healer in the water tribe. She heals warriors who have known battle.", 58 | "dragon_name_str": "Longlu", 59 | "family_str": "blue", 60 | "location_city_str": "hilo", 61 | "location_country_str": "usa", 62 | "location_neighborhood_str": "mona loop", 63 | "location_state_str": "hawaii" 64 | }, 65 | { 66 | "description_str": "Dexler is a protector of the earth and forests. He is as green as the earth and burrows into the ground for protection and extra defense.", 67 | "dragon_name_str": "Dexler", 68 | "family_str": "green", 69 | "location_city_str": "lexington", 70 | "location_country_str": "usa", 71 | "location_neighborhood_str": "bellcastle rd", 72 | "location_state_str": "kentucky" 73 | }, 74 | { 75 | "description_str": "Fireball is a young dragon in training. He is learning how to control his fire, but is still lethal.", 76 | "dragon_name_str": "Fireball", 77 | "family_str": "red", 78 | "location_city_str": "bangor", 79 | "location_country_str": "usa", 80 | "location_neighborhood_str": "stillwater ave", 81 | "location_state_str": "maine" 82 | }, 83 | { 84 | "description_str": "Shadow is one of the stealthiest dragons. He can disappear and is known to assassinate many of his enemies.", 85 | "dragon_name_str": "Shadow", 86 | "family_str": "black", 87 | "location_city_str": "eden", 88 | "location_country_str": "usa", 89 | "location_neighborhood_str": "church st", 90 | "location_state_str": "mississsippi" 91 | }, 92 | { 93 | "description_str": "Herma is a wise water sage dragon. Dragons travel from all the land to seek her counsel and her wisdom.", 94 | "dragon_name_str": "Herma", 95 | "family_str": "blue", 96 | "location_city_str": "twin falls", 97 | "location_country_str": "usa", 98 | "location_neighborhood_str": "applewood dr", 99 | "location_state_str": "idaho" 100 | }, 101 | { 102 | "description_str": "Samurilio wears protective armor that gives him a defense boost in battle. He holds honor for his earth tribe.", 103 | "dragon_name_str": "Samurilio", 104 | "family_str": "green", 105 | "location_city_str": "las vegas", 106 | "location_country_str": "usa", 107 | "location_neighborhood_str": "harmony st", 108 | "location_state_str": "nevada" 109 | }, 110 | { 111 | "description_str": "Firestorm can summon a fire storm of hail and rain, that burns his opponents.", 112 | "dragon_name_str": "Firestorm", 113 | "family_str": "red", 114 | "location_city_str": "tempe", 115 | "location_country_str": "usa", 116 | "location_neighborhood_str": "e laguna dr", 117 | "location_state_str": "arizona" 118 | }, 119 | { 120 | "description_str": "Sonic has black spikes that can penetrate his enemies. He has a spiked tail that can attack his opponents.", 121 | "dragon_name_str": "Sonic", 122 | "family_str": "black", 123 | "location_city_str": "flagstaff", 124 | "location_country_str": "usa", 125 | "location_neighborhood_str": "lake mary rd", 126 | "location_state_str": "arizona" 127 | }, 128 | { 129 | "description_str": "Hydrasha is a double headed dragon. She wields both ice and water.", 130 | "dragon_name_str": "Hydraysha", 131 | "family_str": "blue", 132 | "location_city_str": "san diego", 133 | "location_country_str": "usa", 134 | "location_neighborhood_str": "hazard way", 135 | "location_state_str": "california" 136 | }, 137 | { 138 | "description_str": "Cassidiuma is the personal protector and knight of the dragon queen Methryl. She is the queen's most loved and feared warrior.", 139 | "dragon_name_str": "Cassidiuma", 140 | "family_str": "green", 141 | "location_city_str": "colby", 142 | "location_country_str": "usa", 143 | "location_neighborhood_str": "poplar st", 144 | "location_state_str": "kansas" 145 | }, 146 | { 147 | "description_str": "Isilier is one of the noblest dragon generals in the fire army. His fire attack is one of the strongest in the fleet.", 148 | "dragon_name_str": "Isilier", 149 | "family_str": "red", 150 | "location_city_str": "dover", 151 | "location_country_str": "usa", 152 | "location_neighborhood_str": "shank rd", 153 | "location_state_str": "delaware" 154 | }, 155 | { 156 | "description_str": "Sheblonguh breathes a dark mucus that blinds his enemies permanently and causes paralysis.", 157 | "dragon_name_str": "Sheblonguh", 158 | "family_str": "black", 159 | "location_city_str": "de moines", 160 | "location_country_str": "usa", 161 | "location_neighborhood_str": "maple st", 162 | "location_state_str": "iowa" 163 | }, 164 | { 165 | "description_str": "Frost can create an eternal winter. He is from the ice tribe of Lanzu.", 166 | "dragon_name_str": "Frost", 167 | "family_str": "blue", 168 | "location_city_str": "las vegas", 169 | "location_country_str": "usa", 170 | "location_neighborhood_str": "hills creek dr", 171 | "location_state_str": "nevada" 172 | }, 173 | { 174 | "description_str": "Ragnorl is a rogue dragon, disowned from his own tribe. He can change colors to blend with the earth around him.", 175 | "dragon_name_str": "Ragnorl", 176 | "family_str": "green", 177 | "location_city_str": "chandler", 178 | "location_country_str": "usa", 179 | "location_neighborhood_str": "w german rd", 180 | "location_state_str": "arizona" 181 | }, 182 | { 183 | "description_str": "Eislex flies with the fire sprites. He protects them and is their guardian.", 184 | "dragon_name_str": "Eislex", 185 | "family_str": "red", 186 | "location_city_str": "st. cloud", 187 | "location_country_str": "usa", 188 | "location_neighborhood_str": "breckenridge ave", 189 | "location_state_str": "minnesota" 190 | }, 191 | { 192 | "description_str": "Smolder is a dragon that's bred strictly for battle. Ferocious and fearless, this dragon has an aggressive attack.", 193 | "dragon_name_str": "Smolder", 194 | "family_str": "black", 195 | "location_city_str": "tampa", 196 | "location_country_str": "usa", 197 | "location_neighborhood_str": "ashley st", 198 | "location_state_str": "florida" 199 | }, 200 | { 201 | "description_str": "Protheus is a wise and ancient dragon that serves on the grand council in the sky world. He uses his power to calm those near him.", 202 | "dragon_name_str": "Protheus", 203 | "family_str": "blue", 204 | "location_city_str": "brandon", 205 | "location_country_str": "usa", 206 | "location_neighborhood_str": "e morgan st", 207 | "location_state_str": "florida" 208 | }, 209 | { 210 | "description_str": "Shulmi is the fastest dragon that can escape any battle. She can outmaneuver most enemies.", 211 | "dragon_name_str": "Shulmi", 212 | "family_str": "green", 213 | "location_city_str": "denver", 214 | "location_country_str": "usa", 215 | "location_neighborhood_str": "colfax", 216 | "location_state_str": "colorado" 217 | }, 218 | { 219 | "description_str": "Xanya is the fire tribe's banished general. She broke ranks and has been wandering ever since.", 220 | "dragon_name_str": "Xanya", 221 | "family_str": "red", 222 | "location_city_str": "las vegas", 223 | "location_country_str": "usa", 224 | "location_neighborhood_str": "e clark ave", 225 | "location_state_str": "nevada" 226 | } 227 | ] --------------------------------------------------------------------------------