Created
March 30, 2018 23:32
-
-
Save ratulbasak/24654110a6b309813ae1e1796f72c923 to your computer and use it in GitHub Desktop.
terraform scripts are in s3 bucket, lambda will install terraform, download the zipped scripts into /tmp directory and run terraform apply...
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 os | |
import subprocess | |
import urllib | |
import boto3 | |
import botocore | |
import commands | |
# Version of Terraform that we're using | |
TERRAFORM_VERSION = '0.8.5' | |
# Download URL for Terraform | |
TERRAFORM_DOWNLOAD_URL = ( | |
'https://releases.hashicorp.com/terraform/%s/terraform_%s_linux_amd64.zip' | |
% (TERRAFORM_VERSION, TERRAFORM_VERSION)) | |
# Paths where Terraform should be installed | |
TERRAFORM_DIR = os.path.join('/tmp', 'terraform_%s' % TERRAFORM_VERSION) | |
TERRAFORM_PATH = os.path.join(TERRAFORM_DIR, 'terraform') | |
def check_call(args): | |
"""Wrapper for subprocess that checks if a process runs correctly, | |
and if not, prints stdout and stderr. | |
""" | |
proc = subprocess.Popen(args, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
cwd='/tmp') | |
stdout, stderr = proc.communicate() | |
if proc.returncode != 0: | |
print(stdout) | |
print(stderr) | |
raise subprocess.CalledProcessError( | |
returncode=proc.returncode, | |
cmd=args) | |
def install_terraform(): | |
"""Install Terraform on the Lambda instance.""" | |
# http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html | |
if os.path.exists(TERRAFORM_PATH): | |
return | |
urllib.urlretrieve(TERRAFORM_DOWNLOAD_URL, '/tmp/terraform.zip') | |
# Flags: | |
# '-o' = overwrite existing files without prompting | |
# '-d' = output directory | |
check_call(['unzip', '-o', '/tmp/terraform.zip', '-d', TERRAFORM_DIR]) | |
check_call([TERRAFORM_PATH, '--version']) | |
print ("TERRAFORM_DIR : " + TERRAFORM_DIR) | |
print("TERRAFORM_PATH : " + TERRAFORM_PATH) | |
def apply_terraform_plan(): | |
"""Download a Terraform plan from S3 and run a 'terraform apply'. | |
:param s3_bucket: Name of the S3 bucket where the plan is stored. | |
:param path: Path to the Terraform planfile in the S3 bucket. | |
""" | |
BUCKET_NAME = 'terraform007' | |
KEY = 'files/ec2.zip' | |
s3_client = boto3.client('s3') | |
#Download private key file from secure S3 bucket | |
s3_client.download_file(BUCKET_NAME, KEY, '/tmp/ec2.zip') | |
# UNZIP CODEBASE | |
print(commands.getstatusoutput('unzip /tmp/ec2.zip -d /tmp')) | |
print("list /tmp") | |
print(commands.getstatusoutput('ls /tmp')) | |
print(commands.getstatusoutput('/tmp/terraform_0.8.5/terraform init /tmp/')) | |
print(commands.getstatusoutput('/tmp/terraform_0.8.5/terraform plan /tmp/')) | |
print(commands.getstatusoutput('/tmp/terraform_0.8.5/terraform apply -input=false /tmp/')) | |
def handler(event, context): | |
install_terraform() | |
apply_terraform_plan() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment