Skip to content

Issue#240 create lambda cloud formation template #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Automation/lambda cloudFormation/lambdaCloudFormationDeploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import boto3
import sys
from botocore.exceptions import ClientError

def deployTemplate():
"""Deploy Lambda Function Stack using Cloudformation Template
:param aws_access_key: The AWS key
:param aws_access_secret: The AWS secret
:returns: Response data from the stack deployment, 200 code if created
"""

try:
aws_access_key=sys.argv[1]
aws_access_secret=sys.argv[2]
stack_name=sys.argv[3]

except Exception:
print('\nIncorrect # of system arguments. Please re-check your parameters')
return

try:
# Set up boto3 session
session = boto3.Session(
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_access_secret
)
# CloudFormation call
lambda_client = session.client('cloudformation', region_name='us-west-2')
return lambda_client.create_stack(
StackName=stack_name,
TemplateURL="https://afrasiyab-sprint5.s3-us-west-2.amazonaws.com/template.json",
Capabilities=['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM','CAPABILITY_AUTO_EXPAND']
)
except ClientError as e:
raise(e)

def main():

response_data = deployTemplate()

if response_data != None and response_data['ResponseMetadata']['HTTPStatusCode'] == 200:
print('Response Data: ', response_data)
print('\nOperation Successful')
else:
print('\nAn error occurred, see above error message.')

main()
62 changes: 62 additions & 0 deletions Automation/lambda cloudFormation/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters" : {
"InstanceType" : {
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : ["t2.micro", "m1.small"]
}
},
"Mappings": {
"AWSInstanceType2Arch" : {
"t2.micro" : { "Arch" : "HVM64" },
"m1.small" : { "Arch" : "HVM64" }
},

"AWSRegionArch2AMI" : {
"us-east-1" : {"HVM64" : "ami-0ff8a91507f77f867", "HVMG2" : "ami-0a584ac55a7631c0c"},
"us-west-2" : {"HVM64" : "ami-a0cfeed8", "HVMG2" : "ami-0e09505bc235aa82d"},
"us-west-1" : {"HVM64" : "ami-0bdb828fd58c52235", "HVMG2" : "ami-066ee5fd4a9ef77f1"},
"eu-west-1" : {"HVM64" : "ami-047bb4163c506cd98", "HVMG2" : "ami-0a7c483d527806435"},
"eu-central-1" : {"HVM64" : "ami-0233214e13e500f77", "HVMG2" : "ami-06223d46a6d0661c7"},
"ap-northeast-1" : {"HVM64" : "ami-06cd52961ce9f0d85", "HVMG2" : "ami-053cdd503598e4a9d"},
"ap-southeast-1" : {"HVM64" : "ami-08569b978cc4dfa10", "HVMG2" : "ami-0be9df32ae9f92309"},
"ap-southeast-2" : {"HVM64" : "ami-09b42976632b27e9b", "HVMG2" : "ami-0a9ce9fecc3d1daf8"},
"sa-east-1" : {"HVM64" : "ami-07b14488da8ea02a0", "HVMG2" : "NOT_SUPPORTED"},
"cn-north-1" : {"HVM64" : "ami-0a4eaf6c4454eda75", "HVMG2" : "NOT_SUPPORTED"}
}

},
"Resources" : {
"AppendItemToListFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"Role": { "Fn::GetAtt" : ["Lambda_Role", "Arn"] },
"Code": {
"ZipFile": { "Fn::Join": ["", [
"var response = require('cfn-response');",
"exports.handler = function(event, context) {",
" var responseData = {Value: event.ResourceProperties.List};",
" responseData.Value.push(event.ResourceProperties.AppendedItem);",
" response.send(event, context, response.SUCCESS, responseData);",
"};"
]]}
},
"Runtime": "nodejs8.10"
}
},
"MyEC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId": { "Fn::FindInMap": [ "AWSRegionArch2AMI", { "Ref": "AWS::Region" }, { "Fn::FindInMap": [
"AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch" ] } ]
},
"InstanceType" : { "Ref" : "InstanceType" }
}
}
},
"Outputs" : {

}
}