-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate-stack.py
More file actions
75 lines (66 loc) · 2.24 KB
/
create-stack.py
File metadata and controls
75 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import boto3
import os
import sys
import aws.stack as stack
import aws.parameters as parameters
from cli.argumentsmanager import ArgumentsManager
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIR = "templates"
TEMPLATE_EXT = ".json"
AWS_REGION = "eu-west-1"
session = boto3.session.Session(
region_name=AWS_REGION
)
argumentsManager = ArgumentsManager(parameters.autoParams)
arguments = argumentsManager.parse_args()
application = arguments.Application
environment = arguments.Environment
templateFileName = application + TEMPLATE_EXT
templateFilePath = os.path.sep.join([ROOT_PATH, TEMPLATE_DIR,templateFileName])
try:
with open(templateFilePath, 'r') as templateFile:
templateContent = templateFile.read()
print("Found template file for application: " + application)
except:
sys.stderr.write("Error: Unknown application (template file not found): " + application)
sys.exit(11)
stackName = stack.create_stack_name(application, environment)
if stack.stack_exists(stackName, session):
sys.stderr.write("Error: Stack with name " + stackName + " already exists")
sys.exit(12)
cfClient = session.client('cloudformation')
response = cfClient.validate_template(TemplateBody=templateContent)
print("Stack template validated")
print("Passing cli-arguments as stack parameters")
argumentsManager.add_parameters_as_arguments(response.get('Parameters', None))
args = vars(argumentsManager.get_arguments())
autoParams = parameters.set_auto_params_values(
application,
environment,
argumentsManager.get_auto_params(),
session
)
argParams = parameters.parse_arguments_as_parameters(args)
parameters = autoParams+argParams
debug = True if args.get('Debug') == 'true' else False
print("Creating stack with name: " + stackName)
cfClient.create_stack(
StackName=stackName,
TemplateBody=templateContent,
Parameters=parameters,
DisableRollback=debug,
Tags=[
{
'Key': 'application',
'Value': application
},
{
'Key': 'environment',
'Value': environment
}
]
)
print("Stack creation running...waiting for completion")
waiter = cfClient.get_waiter('stack_create_complete')
waiter.wait(StackName=stackName)
print("Stack created")