Last active
December 6, 2019 03:47
-
-
Save GeneralTesler/df8c1612ac6a7219ab3a48e8000d7fa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json,os,boto3 | |
def lambda_handler(event, context): | |
iid = os.getenv('INSTANCE_ID',None) | |
pd = os.getenv('PAYLOAD_DIR',None) | |
pipeline = boto3.client('codepipeline') | |
job = event['CodePipeline.job']['id'] | |
if iid is None or pd is None: | |
pipeline.put_job_failure_result( | |
jobId=job, | |
failureDetails={ | |
'type':'ConfigurationError', | |
'message':'missing environment variables in lambda' | |
} | |
) | |
artifact = event['CodePipeline.job']['data']['inputArtifacts'][0]['location']['s3Location'] | |
s3loc = 's3://'+artifact['bucketName']+'/'+artifact['objectKey'] | |
#print s3loc | |
zf = artifact['objectKey'].split('/')[-1] | |
cmd1 = 'aws s3 cp %s %s.zip' % (s3loc,zf) | |
cmd2 = 'unzip %s.zip -d %s' % (zf,zf) | |
cmd3 = 'sudo mv %s/bin/* %s' % (zf,pd) | |
client = boto3.client('ssm') | |
response = client.send_command( | |
InstanceIds=[ | |
iid | |
], | |
DocumentName='AWS-RunShellScript', | |
Parameters={ | |
'commands':[cmd1, | |
cmd2, | |
cmd3 | |
], | |
'workingDirectory':[ | |
'/tmp' | |
] | |
} | |
) | |
cmdid = response['Command']['CommandId'] | |
pipeline.put_job_success_result( | |
jobId=job, | |
currentRevision={ | |
'revision':'1', | |
'changeIdentifier':'1' | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment