Skip to content

Commit 41a9e03

Browse files
committed
read in user data from local file. Closes #10
1 parent be3fcf8 commit 41a9e03

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

app/ec2_instance_stack.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os.path
22
import os
33

4-
from aws_cdk.aws_s3_assets import Asset
5-
64
from aws_cdk import (
75
aws_ec2 as ec2,
86
aws_iam as iam,
@@ -46,7 +44,7 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
4644
]
4745
)
4846

49-
# Instance Role and SSM Managed Policy and S3 Managed Policy
47+
# Instance Role and SSM Managed Policy
5048
role = iam.Role(
5149
self,
5250
"InstanceSSM",
@@ -60,31 +58,19 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
6058
)
6159

6260
# Instance
61+
# Script in user data startup script
62+
with open("./configure.sh") as f:
63+
user_data = f.read()
64+
6365
instance = ec2.Instance(
6466
self,
6567
"Instance",
6668
instance_type=instance_type,
6769
machine_image=machine_image,
6870
vpc=vpc,
69-
role=role
70-
)
71-
72-
# Script in S3 as Asset
73-
asset = Asset(
74-
self,
75-
"Asset",
76-
path=os.path.join(dirname, "../configure.sh")
77-
)
78-
local_path = instance.user_data.add_s3_download_command(
79-
bucket=asset.bucket,
80-
bucket_key=asset.s3_object_key
81-
)
82-
83-
# Userdata executes script from S3
84-
instance.user_data.add_execute_file_command(
85-
file_path=local_path
71+
role=role,
72+
user_data=ec2.UserData.custom(user_data)
8673
)
87-
asset.grant_read(instance.role)
8874

8975
# Output data bucket permissions
9076
output_bucket = s3.Bucket.from_bucket_name(

ec2-ssm-s3.png

3.22 KB
Loading

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy