Skip to content
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

closes #735 Add cloud-config.yaml UserData to basic EC2 example #1132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion examples/EC2InstanceSample.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
# Converted from EC2InstanceSample.template located at:
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
from io import open
import os

from troposphere import Base64, FindInMap, GetAtt
from troposphere import Parameter, Output, Ref, Template
import troposphere.ec2 as ec2


def get_user_data(hostname, domain):
""" Open local YAML config template and substitute hostname & domain """
dir_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.join(dir_path, 'cloud-config.yaml')
with open(config_path, encoding='utf-8') as f:
data = f.read()
data = data.replace('${InstanceHostname}', hostname)
data = data.replace('${PublicDomainName}', domain)

return data


template = Template()

keyname_param = template.add_parameter(Parameter(
Expand All @@ -31,7 +45,7 @@
InstanceType="t1.micro",
KeyName=Ref(keyname_param),
SecurityGroups=["default"],
UserData=Base64("80")
UserData=Base64(get_user_data('hostname', 'hostname.example.com')),
))

template.add_output([
Expand Down
26 changes: 26 additions & 0 deletions examples/cloud-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#cloud-config
hostname: ${InstanceHostname}
fqdn: ${InstanceHostname}.${PublicDomainName}
manage_etc_hosts: true
package_update: true
package_upgrade: true
packages:
- build-essential
- curl
- git
runcmd:
- |
echo "-----BEGIN RSA PRIVATE KEY-----
AAAAAAAAAAAAAAAAAAAAAAAAAA
-----END RSA PRIVATE KEY-----
" > /home/ubuntu/.ssh/id_rsa
- chmod 600 /home/ubuntu/.ssh/id_rsa
- chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa
- ssh -oStrictHostKeyChecking=no -T [email protected]
ssh_authorized_keys:
- ssh-rsa AAAA==
- ssh-rsa AAAA==
power_state:
timeout: 120
message: Rebooting to ensure hostname has stuck correctly and apply any kernel updates
mode: reboot
2 changes: 1 addition & 1 deletion tests/examples_output/EC2InstanceSample.template
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"default"
],
"UserData": {
"Fn::Base64": "80"
"Fn::Base64": "#cloud-config\nhostname: hostname\nfqdn: hostname.hostname.example.com\nmanage_etc_hosts: true\npackage_update: true\npackage_upgrade: true\npackages:\n - build-essential\n - curl\n - git\nruncmd:\n - |\n echo \"-----BEGIN RSA PRIVATE KEY-----\n AAAAAAAAAAAAAAAAAAAAAAAAAA\n -----END RSA PRIVATE KEY-----\n \" > /home/ubuntu/.ssh/id_rsa\n - chmod 600 /home/ubuntu/.ssh/id_rsa\n - chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa\n - ssh -oStrictHostKeyChecking=no -T [email protected]\nssh_authorized_keys:\n - ssh-rsa AAAA==\n - ssh-rsa AAAA==\npower_state:\n timeout: 120\n message: Rebooting to ensure hostname has stuck correctly and apply any kernel updates\n mode: reboot\n"
}
},
"Type": "AWS::EC2::Instance"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_example(self):
sys.stdout = stdout
with open(self.filename) as f:
code = compile(f.read(), self.filename, 'exec')
exec(code, {'__name__': '__main__'})
exec(code, {'__name__': '__main__', '__file__': self.filename})
finally:
sys.stdout = saved
# rewind fake stdout so we can read it
Expand Down