Skip to content

Instantly share code, notes, and snippets.

@DanBradbury
Last active September 5, 2018 19:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DanBradbury/9e75fadbbb96250c7cdd25291a2efe37 to your computer and use it in GitHub Desktop.
import yaml,os, json
ENV_PREFIX = 'cc-api-service-beta-'
with open('serverless.yml') as f:
data = yaml.safe_load(f)
#print data['provider']['environment']
env_vars = {}
#env_string = "--environment Variables={first=1"
for env_var in data['provider']['environment'].keys():
env_vars[env_var] = "test_string"
final_object = {"Variables": env_vars}
env_string = json.dumps(final_object)
# TODO: remove bucket name + replace with os.environ.get('S3_BUCKET')
# TODO: same with region info
SHARED_PREFIX = "--region us-west-2 --s3-bucket cc-api-service-beta-serverlessdeploymentbucket-kfpked2dfb2a "
UPDATE_TEMPLATE = (
"aws lambda update-function-code --region us-west-2 --s3-bucket cc-api-service-beta-serverlessdeploymentbucket-kfpked2dfb2a "
"--function-name {function_name} --s3-key upload.zip"
)
UPDATE_ENV_CMD = (
"aws lambda update-function-configuration --region us-west-2 "
"--function-name {function_name} --handler {handler} "
)
# https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html
CREATE_TEMPLATE = (
"aws lambda create-function --region us-west-2 "
"--function-name {function_name} "
"--runtime python2.7 "
"--handler {handler} "
"--code S3Bucket=cc-api-service-beta-serverlessdeploymentbucket-kfpked2dfb2a,S3Key=upload.zip "
"--role "+os.environ.get("ROLE_IAM")
)
for fun in data['functions'].keys():
full_function = ENV_PREFIX+fun
handler_name = data['functions'][fun]['handler']
print 'aws lambda get-function-configuration --function '+full_function
print 'if [ $? -eq 0 ]; then'
# update-function-configuration (environment variables / other config handled at that Lambda level (timers, etc)
print ' '+UPDATE_TEMPLATE.format(function_name=full_function)
print 'else'
print ' '+CREATE_TEMPLATE.format(function_name=full_function, handler=handler_name)
print 'fi'
print UPDATE_ENV_CMD.format(function_name=full_function, handler=handler_name)+"--environment '"+env_string+"'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment