Skip to content

Instantly share code, notes, and snippets.

@walokra
Created March 13, 2020 12:42
Show Gist options
  • Select an option

  • Save walokra/3938dd92b6c9e5d1921f25e3ddc189e4 to your computer and use it in GitHub Desktop.

Select an option

Save walokra/3938dd92b6c9e5d1921f25e3ddc189e4 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: "2010-09-09"
Description: "Stack for creating EBS LVM volume"
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: "AWS::EC2::KeyPair::KeyName"
ConstraintDescription: must be the name of an existing EC2 KeyPair.
InstanceAZ:
Description: EC2 AZ.
Type: AWS::EC2::AvailabilityZone::Name
ConstraintDescription: "Must be the name of an availabity zone."
Default: "eu-central-1a"
InstanceType:
Description: EC2 instance type
Type: String
Default: t2.micro
AllowedValues: [t2.micro]
ConstraintDescription: Please choose a valid instance type.
LinuxAMIID:
Description: The Latest Amazon Linux 2 AMI taken from the public Systems Manager Parameter Store
Type: AWS::SSM::Parameter::Value<String>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
# EC2 Instance with a custom security group
# and an externally created EBS volume attached
Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
volume_config:
- install_packages
- configure_volume
install_packages:
packages:
yum:
xfsprogs: []
configure_volume:
# Using LVM to make it easier to resize logical disk volumes in the future if need be.
# First create an LVM physical volume, then a volume group, then a logical volume.
commands:
1_pvcreate:
command: pvcreate /dev/xvdf
2_vgcreate:
command: vgcreate vg0 /dev/xvdf
3_lvcreate:
command: lvcreate -l 100%FREE -n timescaledb vg0
4_mkfs:
command: mkfs.xfs -f /dev/vg0/timescaledb
5_mkdir:
command: mkdir /var/timescaledb
6_fstab:
command: echo "/dev/mapper/vg0-timescaledb /var/timescaledb xfs defaults,auto,noatime,noexec 0 0" | tee -a /etc/fstab
7_mount:
command: mount -a
8_data:
command: mkdir /var/timescaledb/data
Properties:
InstanceType: !Ref InstanceType
ImageId: !Ref LinuxAMIID
AvailabilityZone: !Ref InstanceAZ
Volumes:
-
VolumeId: !Ref DatabaseVolume
Device: /dev/xvdf
Monitoring: false # Enable Enhanced Monitoring
KeyName: !Ref KeyName
SecurityGroupIds:
- !Ref SSHSecurityGroup
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
UserData:
# See: https://github.com/1Strategy/blog-add-ebs-volume
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource Instance --configsets volume_config --region ${AWS::Region}
/opt/aws/bin/cfn-signal --exit-code $? --stack ${AWS::StackId} --resource Instance --region ${AWS::Region}
DatabaseVolume:
Type: AWS::EC2::Volume
Properties:
Size: 1536
AvailabilityZone: !Ref InstanceAZ
VolumeType: gp2
Tags:
- Key: Name
Value: "TimescaleDB LVM volume"
DeletionPolicy: Retain
Outputs:
VolumeId:
Description: VolumeId of the newly created EBS volume
Value: !Ref DatabaseVolume
InstanceId:
Description: InstanceId of the newly created EC2 instance
Value: !Ref Instance
AZ:
Description: Availability Zone of the newly created EC2 instance
Value: !GetAtt Instance.AvailabilityZone
HostDNS:
Description: Host public DNS
Value: !GetAtt Instance.PublicDnsName
PublicIPAddress:
Description: Host public IP
Value: !GetAtt Instance.PublicIp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment