-
-
Save clstokes/3f732e3a6a4866fb34c061bbc7ce4f80 to your computer and use it in GitHub Desktop.
Pulumi: Python AssumeRole example
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
| # Copyright 2016-2018, Pulumi Corporation. All rights reserved. | |
| import pulumi | |
| from pulumi import resource | |
| from pulumi_aws import provider | |
| from pulumi_aws import ec2 | |
| from ami import get_linux_ami | |
| assume_role_provider = provider.Provider( | |
| "aws", | |
| assume_role={ | |
| "role_arn": "arn:aws:iam::123456789:role/allow-ec2-management-d2ccb5e" | |
| }, | |
| ) | |
| assume_role_resource_options = resource.ResourceOptions(provider=assume_role_provider) | |
| size = "t2.micro" | |
| group = ec2.SecurityGroup( | |
| "web-secgrp", | |
| assume_role_resource_options, | |
| description="Enable HTTP access", | |
| ingress=[ | |
| { | |
| "protocol": "tcp", | |
| "from_port": 80, | |
| "to_port": 80, | |
| "cidr_blocks": ["0.0.0.0/0"], | |
| } | |
| ], | |
| ) | |
| server = ec2.Instance( | |
| "web-server-www", | |
| assume_role_resource_options, | |
| instance_type=size, | |
| security_groups=[group.name], | |
| ami=get_linux_ami(size), | |
| ) | |
| pulumi.export("public_ip", server.public_ip) | |
| pulumi.export("public_dns", server.public_dns) |
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
| $ AWS_ACCESS_KEY_ID=AKIAIABCDEFGHIFKL AWS_SECRET_ACCESS_KEY=AAABBBCCCDDDEEEFFGGHHHIII AWS_REGION=us-east-1 pulumi up | |
| Previewing update (private-pulumi-aws-py-ec2-dev): | |
| Type Name Plan | |
| + pulumi:pulumi:Stack private-pulumi-aws-py-ec2-private-pulumi-aws-py-ec2-dev create | |
| + ├─ pulumi:providers:aws aws create | |
| + ├─ aws:ec2:SecurityGroup web-secgrp create | |
| + └─ aws:ec2:Instance web-server-www create | |
| Resources: | |
| + 4 to create | |
| Do you want to perform this update? yes | |
| Updating (private-pulumi-aws-py-ec2-dev): | |
| Type Name Status | |
| + pulumi:pulumi:Stack private-pulumi-aws-py-ec2-private-pulumi-aws-py-ec2-dev created | |
| + ├─ pulumi:providers:aws aws created | |
| + ├─ aws:ec2:SecurityGroup web-secgrp created | |
| + └─ aws:ec2:Instance web-server-www created | |
| Outputs: | |
| public_dns: "ec2-18-204-204-109.compute-1.amazonaws.com" | |
| public_ip : "18.204.204.109" | |
| Resources: | |
| + 4 created | |
| Duration: 45s | |
| Permalink: https://app.pulumi.com/clstokes/private-pulumi-aws-py-ec2-dev/updates/3 | |
| $ |
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
| # Copyright 2016-2017, Pulumi Corporation. All rights reserved. | |
| from pulumi_aws import config | |
| # This file defines some maps that correspond to the recommended AWS Marketplace values: | |
| # http://docs.aws.amazon.com/servicecatalog/latest/adminguide/catalogs_marketplace-products.html | |
| # Ultimately, this lets us choose a recommended Amazon Linux AMI; see: | |
| # https://aws.amazon.com/amazon-linux-ami/instance-type-matrix/ | |
| instance_type_arch = { | |
| 't1.micro' : 'PV64' , | |
| 't2.nano' : 'HVM64', | |
| 't2.micro' : 'HVM64', | |
| 't2.small' : 'HVM64', | |
| 't2.medium' : 'HVM64', | |
| 't2.large' : 'HVM64', | |
| 'm1.small' : 'PV64' , | |
| 'm1.medium' : 'PV64' , | |
| 'm1.large' : 'PV64' , | |
| 'm1.xlarge' : 'PV64' , | |
| 'm2.xlarge' : 'PV64' , | |
| 'm2.2xlarge' : 'PV64' , | |
| 'm2.4xlarge' : 'PV64' , | |
| 'm3.medium' : 'HVM64', | |
| 'm3.large' : 'HVM64', | |
| 'm3.xlarge' : 'HVM64', | |
| 'm3.2xlarge' : 'HVM64', | |
| 'm4.large' : 'HVM64', | |
| 'm4.xlarge' : 'HVM64', | |
| 'm4.2xlarge' : 'HVM64', | |
| 'm4.4xlarge' : 'HVM64', | |
| 'm4.10xlarge': 'HVM64', | |
| 'c1.medium' : 'PV64' , | |
| 'c1.xlarge' : 'PV64' , | |
| 'c3.large' : 'HVM64', | |
| 'c3.xlarge' : 'HVM64', | |
| 'c3.2xlarge' : 'HVM64', | |
| 'c3.4xlarge' : 'HVM64', | |
| 'c3.8xlarge' : 'HVM64', | |
| 'c4.large' : 'HVM64', | |
| 'c4.xlarge' : 'HVM64', | |
| 'c4.2xlarge' : 'HVM64', | |
| 'c4.4xlarge' : 'HVM64', | |
| 'c4.8xlarge' : 'HVM64', | |
| 'g2.2xlarge' : 'HVMG2', | |
| 'g2.8xlarge' : 'HVMG2', | |
| 'r3.large' : 'HVM64', | |
| 'r3.xlarge' : 'HVM64', | |
| 'r3.2xlarge' : 'HVM64', | |
| 'r3.4xlarge' : 'HVM64', | |
| 'r3.8xlarge' : 'HVM64', | |
| 'i2.xlarge' : 'HVM64', | |
| 'i2.2xlarge' : 'HVM64', | |
| 'i2.4xlarge' : 'HVM64', | |
| 'i2.8xlarge' : 'HVM64', | |
| 'd2.xlarge' : 'HVM64', | |
| 'd2.2xlarge' : 'HVM64', | |
| 'd2.4xlarge' : 'HVM64', | |
| 'd2.8xlarge' : 'HVM64', | |
| 'hi1.4xlarge': 'HVM64', | |
| 'hs1.8xlarge': 'HVM64', | |
| 'cr1.8xlarge': 'HVM64', | |
| 'cc2.8xlarge': 'HVM64', | |
| } | |
| """A map of instance type to its architecture.""" | |
| region_arch_linux_ami = { | |
| 'us-east-1': { | |
| 'PV64' : 'ami-2a69aa47', | |
| 'HVM64': 'ami-6869aa05', | |
| 'HVMG2': 'ami-648d9973', | |
| }, | |
| 'us-west-2': { | |
| 'PV64' : 'ami-7f77b31f', | |
| 'HVM64': 'ami-7172b611', | |
| 'HVMG2': 'ami-09cd7a69', | |
| }, | |
| 'us-west-1': { | |
| 'PV64' : 'ami-a2490dc2', | |
| 'HVM64': 'ami-31490d51', | |
| 'HVMG2': 'ami-1e5f0e7e', | |
| }, | |
| 'eu-west-1': { | |
| 'PV64' : 'ami-4cdd453f', | |
| 'HVM64': 'ami-f9dd458a', | |
| 'HVMG2': 'ami-b4694ac7', | |
| }, | |
| 'eu-west-2': { | |
| 'PV64' : 'NOT_SUPPORTED', | |
| 'HVM64': 'ami-886369ec', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| }, | |
| 'eu-central-1': { | |
| 'PV64' : 'ami-6527cf0a', | |
| 'HVM64': 'ami-ea26ce85', | |
| 'HVMG2': 'ami-de5191b1', | |
| }, | |
| 'ap-northeast-1': { | |
| 'PV64' : 'ami-3e42b65f', | |
| 'HVM64': 'ami-374db956', | |
| 'HVMG2': 'ami-df9ff4b8', | |
| }, | |
| 'ap-northeast-2': { | |
| 'PV64' : 'NOT_SUPPORTED', | |
| 'HVM64': 'ami-2b408b45', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| }, | |
| 'ap-southeast-1': { | |
| 'PV64' : 'ami-df9e4cbc', | |
| 'HVM64': 'ami-a59b49c6', | |
| 'HVMG2': 'ami-8d8d23ee', | |
| }, | |
| 'ap-southeast-2': { | |
| 'PV64' : 'ami-63351d00', | |
| 'HVM64': 'ami-dc361ebf', | |
| 'HVMG2': 'ami-cbaf94a8', | |
| }, | |
| 'ap-south-1': { | |
| 'PV64' : 'NOT_SUPPORTED', | |
| 'HVM64': 'ami-ffbdd790', | |
| 'HVMG2': 'ami-decdbab1', | |
| }, | |
| 'us-east-2': { | |
| 'PV64' : 'NOT_SUPPORTED', | |
| 'HVM64': 'ami-f6035893', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| }, | |
| 'ca-central-1': { | |
| 'PV64' : 'NOT_SUPPORTED', | |
| 'HVM64': 'ami-730ebd17', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| }, | |
| 'sa-east-1': { | |
| 'PV64' : 'ami-1ad34676', | |
| 'HVM64': 'ami-6dd04501', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| }, | |
| 'cn-north-1': { | |
| 'PV64' : 'ami-77559f1a', | |
| 'HVM64': 'ami-8e6aa0e3', | |
| 'HVMG2': 'NOT_SUPPORTED', | |
| } | |
| } | |
| """A map from region to inner maps from architecture to the recommended Linux AMI.""" | |
| def get_linux_ami(instance_type): | |
| """Gets the recommended Linux AMI for the given instance in the current AWS region.""" | |
| region = config.region | |
| arch = instance_type_arch[instance_type] | |
| return region_arch_linux_ami[region][arch] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment