Skip to content

Instantly share code, notes, and snippets.

@mikepietruszka
Last active February 24, 2020 15:36
Show Gist options
  • Save mikepietruszka/3f31b4c4b4e501c9cd1577fa104a4eea to your computer and use it in GitHub Desktop.
Save mikepietruszka/3f31b4c4b4e501c9cd1577fa104a4eea to your computer and use it in GitHub Desktop.
Sample step function with map processing support: get status code from HTTPS requests and store them in AWS SSM Parameter Store
#!/usr/bin/env python
import boto3
import json
import logging
import requests
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
# TODO implement
# url = "www.google.com" # Skip http|https
url = event
fixed_url = "https://{0}".format(url)
response = requests.get(fixed_url)
return_response = {}
return_response['url'] = url
return_response['status_code'] = response.status_code
return return_response
{
"proceed": "true",
"request": {
"urls": [
"www.usatoday.com",
"www.cnn.com",
"www.foxnews.com"
]
}
}
#!/usr/bin/env python
import boto3
import json
import logging
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
url = event['url']
status_code = event['status_code']
session = boto3.Session(region_name='')
ssm = session.client('ssm')
parameter_path = "/responses/{0}/status_code".format(url)
print(parameter_path)
parameter = ssm.put_parameter(
Name=parameter_path,
Value=str(status_code),
Type='String',
Overwrite=True)
logger.info(parameter)
return parameter
{
"Comment": "Simple example that gets status code and puts in SSM Parameter Store",
"StartAt": "Ask for confirmation",
"States": {
"Ask for confirmation": {
"Comment": "Ask whether to get web status code",
"Type": "Choice",
"Choices": [
{
"Variable": "$.proceed",
"BooleanEquals": true,
"Next": "Yes"
},
{
"Variable": "$.proceed",
"BooleanEquals": false,
"Next": "No"
}
],
"Default": "Yes"
},
"Yes":{
"Type": "Pass",
"Next": "get-web-status-for-all"
},
"No": {
"Type": "Fail",
"Cause": "Declined to proceed"
},
"get-web-status-for-all": {
"Type": "Map",
"InputPath": "$.request",
"ItemsPath": "$.urls",
"MaxConcurrency": 2,
"End": true,
"Iterator": {
"StartAt": "get-web-status",
"States": {
"get-web-status": {
"Type": "Task",
"Resource": "arn:aws:lambda:::function:get-web-status",
"ResultPath": "$",
"Next": "put-web-status"
},
"put-web-status": {
"Comment": "Puts lambda output into SSM",
"Type": "Task",
"Resource": "arn:aws:lambda:::function:put-web-status",
"End": true
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment