-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add macro deploy #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ggiallo28
wants to merge
4
commits into
cloudtools:main
Choose a base branch
from
ggiallo28:macro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add macro deploy #1701
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Troposphere Macro | ||
|
|
||
| The `Troposphere` macro adds the ability to create CloudFormation resources from troposphere stacks. | ||
|
|
||
| # How to install and use the Macro in your AWS account | ||
|
|
||
| ## Deploying | ||
|
|
||
| 1. You will need an S3 bucket to store the CloudFormation artifacts: | ||
| * If you don't have one already, create one with `aws s3 mb s3://<bucket name>` | ||
|
|
||
| 2. Install all python requirements | ||
|
|
||
| ```shell | ||
| pip install -r source/requirements.txt -t source | ||
| ``` | ||
|
|
||
| 3. Package the CloudFormation template. The provided template uses [the AWS Serverless Application Model](https://aws.amazon.com/about-aws/whats-new/2016/11/introducing-the-aws-serverless-application-model/) so must be transformed before you can deploy it. | ||
|
|
||
| ```shell | ||
| aws cloudformation package \ | ||
| --template-file template.yaml \ | ||
| --s3-bucket <your bucket name here> \ | ||
| --output-template-file template.output | ||
| ``` | ||
|
|
||
| 4. Deploy the packaged CloudFormation template to a CloudFormation stack: | ||
|
|
||
| ```shell | ||
| aws cloudformation deploy \ | ||
| --stack-name troposphere-macro \ | ||
| --template-file template.output \ | ||
| --capabilities CAPABILITY_IAM | ||
| ``` | ||
|
|
||
| 5. To test out the macro's capabilities, try launching the provided example template: | ||
|
|
||
| ```shell | ||
| aws cloudformation deploy \ | ||
| --stack-name template-macro-example \ | ||
| --template-file example.py | ||
| ``` | ||
|
|
||
| ## Usate | ||
|
|
||
| Just add your resources to macro_template object, the template object is created by macro itself. | ||
| You can provide your troposphere code in Troposphere tag. | ||
|
|
||
| ``` | ||
| Transform: [Troposhere] | ||
| Description: Example Macro Troposhere. | ||
|
|
||
| Parameters: | ||
| InstanceName: | ||
| Type: String | ||
| Default: "MyInstance" | ||
| ImageId: | ||
| Type: String | ||
| Default: "ami-951945d0" | ||
| InstanceType: | ||
| Type: String | ||
| Default: "t1.micro" | ||
|
|
||
| Troposhere: | | ||
| from troposphere import Ref | ||
| import troposphere.ec2 as ec2 | ||
|
|
||
| instance = ec2.Instance('MyInstance') | ||
|
|
||
| instance.ImageId = Ref('ImageId') | ||
| instance.InstanceType = Ref('InstanceType') | ||
| instance.Tags = ec2.Tags(Name = Ref('InstanceName')) | ||
|
|
||
| macro_template.add_resource(instance) | ||
| ``` | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| Transform: [Troposhere] | ||
| Description: Example Macro Troposhere. | ||
|
|
||
| Parameters: | ||
| InstanceName: | ||
| Type: String | ||
| Default: "MyInstance" | ||
| ImageId: | ||
| Type: String | ||
| Default: "ami-eb0c7791" | ||
| InstanceType: | ||
| Type: String | ||
| Default: "t1.micro" | ||
|
|
||
| Troposhere: | | ||
| from troposphere import Ref | ||
| import troposphere.ec2 as ec2 | ||
|
|
||
| instance = ec2.Instance('MyInstance') | ||
|
|
||
| instance.ImageId = Ref('ImageId') | ||
| instance.InstanceType = Ref('InstanceType') | ||
| instance.Tags = ec2.Tags(Name = Ref('InstanceName')) | ||
|
|
||
| macro_template.add_resource(instance) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import sys, os, io, traceback | ||
| from troposphere.template_generator import TemplateGenerator | ||
|
|
||
| def handle_template(template): | ||
| troposphere_code = template["Troposhere"] | ||
| del template["Troposhere"] | ||
|
|
||
| macro_template = TemplateGenerator(template) | ||
| exec(troposphere_code) | ||
|
|
||
| return macro_template.to_json() | ||
|
|
||
| def handler(event, context): | ||
| request_id = event['requestId'] | ||
| parameters = event['templateParameterValues'] | ||
|
|
||
| macro_response = { | ||
| 'status': 'success', | ||
| 'requestId': request_id | ||
| } | ||
|
|
||
| try: | ||
| macro_response['fragment'] = handle_template(event['fragment']) | ||
| except Exception as e: | ||
| traceback.print_exc() | ||
| macro_response['status'] = 'failure' | ||
| macro_response['errorMessage'] = str(e) | ||
|
|
||
| return macro_response |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| troposphere[policy] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| Transform: AWS::Serverless-2016-10-31 | ||
|
|
||
| Resources: | ||
| TransformExecutionRole: | ||
| Type: AWS::IAM::Role | ||
| Properties: | ||
| AssumeRolePolicyDocument: | ||
| Version: 2012-10-17 | ||
| Statement: | ||
| - Effect: Allow | ||
| Principal: | ||
| Service: | ||
| - lambda.amazonaws.com | ||
| Action: | ||
| - 'sts:AssumeRole' | ||
| Path: / | ||
| Policies: | ||
| - PolicyName: root | ||
| PolicyDocument: | ||
| Version: 2012-10-17 | ||
| Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - 'logs:*' | ||
| Resource: 'arn:aws:logs:*:*:*' | ||
|
|
||
| MacroFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Runtime: python3.6 | ||
| CodeUri: source/ | ||
| Handler: macro.handler | ||
| Role: !GetAtt TransformExecutionRole.Arn | ||
| Timeout: 300 | ||
|
|
||
| Macro: | ||
| Type: AWS::CloudFormation::Macro | ||
| Properties: | ||
| Name: Troposhere | ||
| FunctionName: !GetAtt MacroFunction.Arn |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.