Skip to content

Commit

Permalink
Merge pull request #200 from stefanjenkner/reorg-cf-stack
Browse files Browse the repository at this point in the history
Restructuring to support launch templates for different distros
  • Loading branch information
stefanjenkner authored May 20, 2024
2 parents 72e47e9 + b990acb commit 000506a
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
- name: Lint
run: ruff check .
- name: Generate template
run: python deploy.py --print-only > jammy-sunshine.template
run: python deploy.py --print-only > ec2-gaming-sunshine.template
- name: Release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: jammy-sunshine.template
files: ec2-gaming-sunshine.template
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NOTES.md
venv/
venv/
ec2-gaming-sunshine.template
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ When updating CloudFormation stack, passing parameters is not required and exist

Launch spot instance:

aws ec2 run-instances --launch-template LaunchTemplateName=jammy-sunshine-spot,Version=\$Latest
aws ec2 run-instances --launch-template LaunchTemplateName=ec2-gaming-sunshine-jammy-spot,Version=\$Latest

Launch on-demand instance:

aws ec2 run-instances --launch-template LaunchTemplateName=jammy-sunshine-on-demand,Version=\$Latest
aws ec2 run-instances --launch-template LaunchTemplateName=ec2-gaming-sunshine-jammy-on-demand,Version=\$Latest

Launch on-demand instance with custom instance type:

aws ec2 run-instances --launch-template LaunchTemplateName=jammy-sunshine-on-demand,Version=\$Latest \
aws ec2 run-instances --launch-template LaunchTemplateName=ec2-gaming-sunshine-jammy-on-demand,Version=\$Latest \
--instance-type g5.4xlarge

By default, access to the EC2 instance is restriced. To update ane set the whitelisted IP address to the IP address of the caller:
By default, access to the EC2 instance is restriced. To update the whitelisted IP address to the IP address of the caller:

./update-ip.py

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ write_files:
- path: /etc/profile.d/restic.sh
content: |
#!/bin/sh
export RESTIC_REPOSITORY=s3:https://s3.eu-central-1.amazonaws.com/jammy-sunshine-library
export RESTIC_REPOSITORY=s3:https://s3.eu-central-1.amazonaws.com/ec2-gaming-sunshine-library
export RESTIC_PASSWORD=sunshine
permissions: '0644'
- path: /usr/local/bin/backup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Parameters:
KeyPair:
Description: EC2 Key Pair
Type: "AWS::EC2::KeyPair::KeyName"
LatestAmiId:
LatestAmiIdJammy:
Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>"
Default: "/aws/service/canonical/ubuntu/server/jammy/stable/current/amd64/hvm/ebs-gp2/ami-id"

Expand Down Expand Up @@ -154,10 +154,10 @@ Resources:
FromPort: 27036
ToPort: 27037
CidrIp: 0.0.0.0/0
LaunchTemplateSpot:
LaunchTemplateJammySpot:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateName: !Sub "${AWS::StackName}-spot"
LaunchTemplateName: !Sub "${AWS::StackName}-jammy-spot"
LaunchTemplateData:
IamInstanceProfile:
Arn: !GetAtt InstanceProfile.Arn
Expand All @@ -179,7 +179,7 @@ Resources:
DeleteOnTermination: true
Encrypted: true
DeviceName: /dev/sda1
ImageId: !Ref LatestAmiId
ImageId: !Ref LatestAmiIdJammy
InstanceType: g4dn.xlarge
KeyName: !Ref KeyPair
InstanceMarketOptions:
Expand All @@ -192,12 +192,18 @@ Resources:
Tags:
- Key: Name
Value: !Sub "${AWS::StackName}-instance"
- Key: Distribution
Value: Ubuntu
- Key: Release
Value: 22.04
- Key: Codename
Value: jammy
UserData: |
{{ cloud_config }}
LaunchTemplateOnDemand:
{{ cloud_config["jammy"] }}
LaunchTemplateJammyOnDemand:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateName: !Sub "${AWS::StackName}-on-demand"
LaunchTemplateName: !Sub "${AWS::StackName}-jammy-on-demand"
LaunchTemplateData:
IamInstanceProfile:
Arn: !GetAtt InstanceProfile.Arn
Expand All @@ -219,16 +225,22 @@ Resources:
DeleteOnTermination: true
Encrypted: true
DeviceName: /dev/sda1
ImageId: !Ref LatestAmiId
ImageId: !Ref LatestAmiIdJammy
InstanceType: g4dn.xlarge
KeyName: !Ref KeyPair
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: !Sub "${AWS::StackName}-instance"
- Key: Distribution
Value: Ubuntu
- Key: Release
Value: 22.04
- Key: Codename
Value: jammy
UserData: |
{{ cloud_config }}
{{ cloud_config["jammy"] }}
LibraryBucket:
Type: AWS::S3::Bucket
Properties:
Expand Down
2 changes: 1 addition & 1 deletion connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import boto3

STACK_NAME = "jammy-sunshine"
STACK_NAME = "ec2-gaming-sunshine"
DEFAULT_APP = "Low Res Desktop"

if platform == "linux" or platform == "linux2":
Expand Down
18 changes: 10 additions & 8 deletions deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from jinja2 import Template

STACK_NAME = "jammy-sunshine"
STACK_NAME = "ec2-gaming-sunshine"
DEFAULT_KEYPAIR = "ec2-gaming"
INITIAL_WHITELISTED_IP = "127.0.0.1/32"

Expand All @@ -22,15 +22,17 @@ def main():

args = parser.parse_args()

with open("cloudformation/jammy-sunshine.yaml") as cf_:
with open("cloudformation/ec2-gaming-sunshine.yaml") as cf_:
template = Template(cf_.read())

with open("cloud-init/cloud-config.yaml", "rb") as cloud_config_:
cloud_config_b64_ = base64.b64encode(cloud_config_.read())
cloud_config_b64_text_ = cloud_config_b64_.decode("ascii")
cloud_config = {}
for flavour in ["jammy"]:
with open(f"cloud-init/cloud-config-{flavour}.yaml", "rb") as cloud_config_:
cloud_config_b64_ = base64.b64encode(cloud_config_.read())
cloud_config[flavour] = cloud_config_b64_.decode("ascii")

if args.print_only:
print(template.render(cloud_config=cloud_config_b64_text_))
print(template.render(cloud_config=cloud_config))
return

logging.basicConfig(level=logging.INFO)
Expand All @@ -56,7 +58,7 @@ def main():
try:
response = client.update_stack(
StackName=STACK_NAME,
TemplateBody=template.render(cloud_config=cloud_config_b64_text_),
TemplateBody=template.render(cloud_config=cloud_config),
Parameters=[
{"ParameterKey": "MyIp", "UsePreviousValue": True},
{"ParameterKey": "KeyPair", "UsePreviousValue": True},
Expand All @@ -75,7 +77,7 @@ def main():
logging.info(f"Creating stack {STACK_NAME}")
response = client.create_stack(
StackName=STACK_NAME,
TemplateBody=template.render(cloud_config=cloud_config_b64_text_),
TemplateBody=template.render(cloud_config=cloud_config),
Parameters=[
{"ParameterKey": "MyIp", "ParameterValue": INITIAL_WHITELISTED_IP},
{"ParameterKey": "KeyPair", "ParameterValue": args.keypair},
Expand Down
3 changes: 2 additions & 1 deletion start.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import boto3

STACK_NAME = "ec2-gaming-sunshine"

def main():

client = boto3.client("ec2")
ec2 = boto3.resource("ec2")
response = client.describe_instances(
Filters=[
{"Name": "tag:Name", "Values": ["jammy-sunshine-instance"]},
{"Name": "tag:Name", "Values": [f"{STACK_NAME}-instance"]},
{"Name": "instance-state-name", "Values": ["stopped"]},
]
)
Expand Down
3 changes: 2 additions & 1 deletion stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import boto3

STACK_NAME = "ec2-gaming-sunshine"

def main():

client = boto3.client("ec2")
ec2 = boto3.resource("ec2")
response = client.describe_instances(
Filters=[
{"Name": "tag:Name", "Values": ["jammy-sunshine-instance"]},
{"Name": "tag:Name", "Values": [f"{STACK_NAME}-instance"]},
{"Name": "instance-state-name", "Values": ["running", "pending"]},
]
)
Expand Down
4 changes: 1 addition & 3 deletions update-ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import botocore
import boto3


STACK_NAME = "jammy-sunshine"

STACK_NAME = "ec2-gaming-sunshine"

def main():

Expand Down

0 comments on commit 000506a

Please sign in to comment.