55Known limitations: cannot transform CodeUri pointing at local directory.
66
77Usage:
8- sam-translate.py --input-file=sam-template.yaml [--output-file=<o>]
8+ sam-translate.py --template-file=sam-template.yaml [--verbose] [--output-template=<o>]
9+ sam-translate.py package --template-file=sam-template.yaml --s3-bucket=my-bucket [--verbose] [--output-template=<o>]
10+ sam-translate.py deploy --template-file=sam-template.yaml --s3-bucket=my-bucket --capabilities=CAPABILITY_NAMED_IAM --stack-name=my-stack [--verbose] [--output-template=<o>]
911
1012Options:
11- --input-file=<i> Location of SAM template to transform.
12- --output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json].
13+ --template-file=<i> Location of SAM template to transform [default: template.yaml].
14+ --output-template=<o> Location to store resulting CloudFormation template [default: transformed-template.json].
15+ --s3-bucket=<s> S3 bucket to use for SAM artifacts when using the `package` command
16+ --capabilities=<c> Capabilities
17+ --stack-name=<n> Unique name for your CloudFormation Stack
18+ --verbose Enables verbose logging
1319
1420"""
1521import json
22+ import logging
1623import os
24+ import platform
25+ import subprocess
26+ import sys
1727
1828import boto3
1929from docopt import docopt
2030
31+ my_path = os .path .dirname (os .path .abspath (__file__ ))
32+ sys .path .insert (0 , my_path + '/..' )
33+
2134from samtranslator .public .translator import ManagedPolicyLoader
2235from samtranslator .translator .transform import transform
2336from samtranslator .yaml_helper import yaml_parse
2437from samtranslator .model .exceptions import InvalidDocumentException
2538
26-
39+ LOG = logging . getLogger ( __name__ )
2740cli_options = docopt (__doc__ )
2841iam_client = boto3 .client ('iam' )
2942cwd = os .getcwd ()
3043
44+ if cli_options .get ('--verbose' ):
45+ logging .basicConfig (level = logging .DEBUG )
46+ else :
47+ logging .basicConfig ()
48+
49+ def execute_command (command , args ):
50+ try :
51+ aws_cmd = 'aws' if platform .system ().lower () != 'windows' else 'aws.cmd'
52+ command_with_args = [aws_cmd , 'cloudformation' , command ] + list (args )
53+
54+ LOG .debug ("Executing command: %s" , command_with_args )
55+
56+ subprocess .check_call (command_with_args )
57+
58+ LOG .debug ("Command successful" )
59+ except subprocess .CalledProcessError as e :
60+ # Underlying aws command will print the exception to the user
61+ LOG .debug ("Exception: %s" , e )
62+ sys .exit (e .returncode )
63+
3164
3265def get_input_output_file_paths ():
33- input_file_option = cli_options .get ('--input -file' )
34- output_file_option = cli_options .get ('--output-file ' )
66+ input_file_option = cli_options .get ('--template -file' )
67+ output_file_option = cli_options .get ('--output-template ' )
3568 input_file_path = os .path .join (cwd , input_file_option )
3669 output_file_path = os .path .join (cwd , output_file_option )
3770
3871 return input_file_path , output_file_path
3972
4073
41- def main ():
42- input_file_path , output_file_path = get_input_output_file_paths ()
74+ def package (input_file_path , output_file_path ):
75+ template_file = input_file_path
76+ package_output_template_file = input_file_path + '._sam_packaged_.yaml'
77+ s3_bucket = cli_options .get ('--s3-bucket' )
78+ args = [
79+ '--template-file' ,
80+ template_file ,
81+ '--output-template-file' ,
82+ package_output_template_file ,
83+ '--s3-bucket' ,
84+ s3_bucket
85+ ]
4386
87+ execute_command ('package' , args )
88+
89+ return package_output_template_file
90+
91+
92+ def transform_template (input_file_path , output_file_path ):
4493 with open (input_file_path , 'r' ) as f :
4594 sam_template = yaml_parse (f )
4695
@@ -56,10 +105,37 @@ def main():
56105 print ('Wrote transformed CloudFormation template to: ' + output_file_path )
57106 except InvalidDocumentException as e :
58107 errorMessage = reduce (lambda message , error : message + ' ' + error .message , e .causes , e .message )
59- print (errorMessage )
108+ LOG . error (errorMessage )
60109 errors = map (lambda cause : cause .message , e .causes )
61- print (errors )
110+ LOG .error (errors )
111+
112+
113+ def deploy (template_file ):
114+ capabilities = cli_options .get ('--capabilities' )
115+ stack_name = cli_options .get ('--stack-name' )
116+ args = [
117+ '--template-file' ,
118+ template_file ,
119+ '--capabilities' ,
120+ capabilities ,
121+ '--stack-name' ,
122+ stack_name
123+ ]
124+
125+ execute_command ('deploy' , args )
126+
127+ return package_output_template_file
62128
63129
64130if __name__ == '__main__' :
65- main ()
131+ input_file_path , output_file_path = get_input_output_file_paths ()
132+
133+ if cli_options .get ('package' ):
134+ package_output_template_file = package (input_file_path , output_file_path )
135+ transform_template (package_output_template_file , output_file_path )
136+ elif cli_options .get ('deploy' ):
137+ package_output_template_file = package (input_file_path , output_file_path )
138+ transform_template (package_output_template_file , output_file_path )
139+ deploy (output_file_path )
140+ else :
141+ transform_template (input_file_path , output_file_path )
0 commit comments