Skip to content

Instantly share code, notes, and snippets.

@toddlers
Created January 12, 2021 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddlers/a1d780188cc1cdd39e732257380d5deb to your computer and use it in GitHub Desktop.
Save toddlers/a1d780188cc1cdd39e732257380d5deb to your computer and use it in GitHub Desktop.
ec2 userdata cloudformation
---
AWSTemplateFormatVersion: '2010-09-09'
Description: Persistantly sets the hostname of a RHEL7 EC2 instance on start
Mappings:
RegionMap:
us-east-1:
'32': ami-6411e20d
'64': ami-7a11e213
us-west-1:
'32': ami-c9c7978c
'64': ami-cfc7978a
eu-west-1:
'32': ami-37c2f643
'64': ami-31c2f645
ap-southeast-1:
'32': ami-66f28c34
'64': ami-60f28c32
ap-northeast-1:
'32': ami-9c03a89d
'64': ami-a003a8a1
Parameters:
SSHKey:
Description: Select your SSH Key
Type: AWS::EC2::KeyPair::KeyName
InstanceTypeParameter:
Type: String
Default: m4.xlarge
AllowedValues:
- t2.micro
- m4.xlarge
- m1.large
Description: Select Instancetype. Default is m4.xlarge.
InstanceHostname:
Description: Select your you instance Hostname
Type: String
Default: RedHatDevInstance
InstanceDomainName:
Description: Select your instance DomainName
Type: String
Default: redhat1.org
Resources:
MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId:
Fn::FindInMap:
- RegionMap
- Ref: AWS::Region
- '64'
InstanceType:
Ref: InstanceTypeParameter
KeyName:
Ref: SSHKey
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash\n"
- "# Replace this with your domain\n"
- DOMAIN=
- Ref: InstanceDomainName
- "\n"
- HOSTNAME=
- Ref: InstanceHostname
- "\n"
- IPV4=/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4
- "\n"
- "# Set the host name\n"
- hostname
- " "
- Ref: InstanceHostname
- "\n"
- echo
- " "
- Ref: InstanceHostname
- "> /etc/hostname\n"
- "# Add fqdn to hosts file\n"
- echo 'HOSTNAME=
- Ref: InstanceHostname
- "'"
- ">> /etc/sysconfig/network\n"
- 'echo ''preserve_hostname: true'' >> /etc/cloud/cloud.cfg
'
- 'cat<<EOF > /etc/hosts
'
- '127.0.0.1 localhost
'
- "$IPV4 $HOSTNAME.$DOMAIN $HOSTNAME\n"
- EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment