Created
April 3, 2020 16:37
-
-
Save ericjsilva/5cebf74ec5dbfbab5f5d979b5d21d687 to your computer and use it in GitHub Desktop.
Sample files for packaging and optionally deploying a Python project to AWS Lambda
This file contains hidden or 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
| #!/usr/bin/env python | |
| import os | |
| import shutil | |
| import datetime | |
| import tempfile | |
| PKG_FILENAME = 'my_lambda_function-{}.zip' | |
| def zipdir(path, ziph): | |
| # ziph is zipfile handle | |
| for root, dirs, files in os.walk(path): | |
| for file in files: | |
| ziph.write(os.path.join(root, file)) | |
| def create_filename(): | |
| ts = datetime.datetime.now().strftime('%y%m%d%H%M') | |
| filename = PKG_FILENAME.format(ts) | |
| return filenam | |
| if __name__ == '__main__': | |
| pkg_dir = './dist' | |
| tmpdir = tempfile.mkdtemp() | |
| if os.path.exists(pkg_dir): | |
| shutil.rmtree(pkg_dir) | |
| os.makedirs(pkg_dir) | |
| pkg_file = create_filename() | |
| try: | |
| tmparchive = os.path.join(tmpdir, pkg_file) | |
| root_dir = "." | |
| dist_file = os.path.join(pkg_dir, pkg_file) | |
| shutil.move(shutil.make_archive(tmparchive, 'zip', root_dir), dist_file) | |
| print('Packaged Application: ' + dist_file) | |
| finally: | |
| shutil.rmtree(tmpdir) |
This file contains hidden or 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
| #!/usr/bin/env python | |
| import os | |
| import shutil | |
| import datetime | |
| import tempfile | |
| import boto3 | |
| boto3.setup_default_session(profile_name='saml') | |
| S3_BUCKET = 'mybucket' | |
| LAMBDA_FUNCTION_NAME = 'my_lambda_function' | |
| S3_BUCKET_PATH = 'aws_lambda/{}/'.format(LAMBDA_FUNCTION_NAME) | |
| PKG_FILENAME = 'my_lambda_function-{}.zip' | |
| PUBLISH_URL = 'https://s3.amazonaws.com/{0}/{1}'.format(S3_BUCKET, S3_BUCKET_PATH) | |
| FUNCTION_ARN = 'arn:aws:lambda:us-east-1:1234567890:function:{}'.format(LAMBDA_FUNCTION_NAME) | |
| DEPLOY = True | |
| def zipdir(path, ziph): | |
| # ziph is zipfile handle | |
| for root, dirs, files in os.walk(path): | |
| for file in files: | |
| ziph.write(os.path.join(root, file)) | |
| def create_filename(): | |
| ts = datetime.datetime.now().strftime('%y%m%d%H%M') | |
| filename = PKG_FILENAME.format(ts) | |
| return filename | |
| def publish_to_s3(filename, filepath): | |
| s3 = boto3.resource('s3') | |
| s3.Object(S3_BUCKET, S3_BUCKET_PATH + filename).put(Body=open(filepath, 'rb')) | |
| def update_function(deployment_file): | |
| lmb = boto3.client('lambda') | |
| r = lmb.update_function_code(FunctionName=LAMBDA_FUNCTION_NAME, | |
| S3Bucket=S3_BUCKET, | |
| S3Key=S3_BUCKET_PATH + deployment_file) | |
| if 'LastUpdateStatus' in r: | |
| print('Deployment status: {status}'.format(status=r['LastUpdateStatus'])) | |
| if __name__ == '__main__': | |
| pkg_dir = './dist' | |
| tmpdir = tempfile.mkdtemp() | |
| if os.path.exists(pkg_dir): | |
| shutil.rmtree(pkg_dir) | |
| os.makedirs(pkg_dir) | |
| pkg_file = create_filename() | |
| try: | |
| tmparchive = os.path.join(tmpdir, pkg_file) | |
| root_dir = "." | |
| dist_file = os.path.join(pkg_dir, pkg_file) | |
| shutil.move(shutil.make_archive(tmparchive, 'zip', root_dir), dist_file) | |
| print('Packaged Application: ' + dist_file) | |
| publish_to_s3(pkg_file, dist_file) | |
| print('Published {0} to S3 bucket'.format(pkg_file)) | |
| print('URL: {0}{1}'.format(PUBLISH_URL, pkg_file)) | |
| if DEPLOY is True: | |
| update_function(pkg_file) | |
| finally: | |
| shutil.rmtree(tmpdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment