
Deploy an agent demo to EC2: no SSH, one-command teardown
One boto3 script for hackathon demos: least-privilege IAM, secrets in Parameter Store, IMDSv2, Session Manager and a --destroy flag.
Hackathon judges need a live URL. You need that URL to cost almost nothing, leak no secrets, and disappear cleanly when judging ends. For my Tripwire agent demo, I wrote one Python script that deploys the whole thing to a single EC2 instance and removes every resource with one flag:
python -m deploy.deploy_ec2 # create or redeploy, prints the URLpython -m deploy.deploy_ec2 --destroy # delete everything it createdThere are no SSH keys, port 22 is never open, and no secret ends up in user data or Git. Here's how each piece works, so you can reuse the pattern for your own demo. The full script is in MdIbuA/strands-tripwire .
What gets created
Everything is tagged
Project=tripwire , which is what makes teardown reliable:- An IAM role and instance profile with only the permissions the app needs
- Two SSM Parameter Store SecureString parameters: an API key and a generated demo password
- A private S3 bucket holding the code bundle
- A security group that opens port 80 and nothing else
- One t3.small Amazon Linux 2023 instance running the app under systemd
1. A role that can do exactly three things
The instance role gets an inline policy with three statements: call Bedrock inference, read the parameters under
/tripwire/* , and read objects from one bucket.{"Sid": "TripwireParams", "Effect": "Allow", "Action": "ssm:GetParameter", "Resource": f"arn:aws:ssm:{REGION}:{ACCOUNT}:parameter/tripwire/*"},{"Sid": "CodeBundle", "Effect": "Allow", "Action": "s3:GetObject", "Resource": f"arn:aws:s3:::{BUCKET}/*"},It also attaches the managed
AmazonSSMManagedInstanceCore policy. That's what replaces SSH: you open a shell through Session Manager, which needs no key pair and no inbound port.One gotcha: a new instance profile can take a few seconds to become usable, and
run_instances fails with "Invalid IAM Instance Profile" in the meantime. The script retries up to five times instead of failing on the first attempt.2. Secrets live in Parameter Store, not in user data
User data is readable by anyone who can describe the instance, so secrets never go there. The script writes the API key and a generated password as SecureString parameters:
ssm.put_parameter(Name=PARAM_KEY, Value=key, Type="SecureString", Overwrite=True)At startup, the app's launcher script reads them through the instance role:
export DECIDER_API_KEY=$(aws ssm get-parameter --name /tripwire/decider-api-key \ --with-decryption --query Parameter.Value --output text)The password is generated once with
secrets.token_urlsafe and saved to a git-ignored local file, so redeploys keep the same password.3. Ship exactly what's committed
The code goes to a private bucket as
git archive HEAD . Public access is blocked on the bucket at creation time:s3.put_public_access_block(Bucket=BUCKET, PublicAccessBlockConfiguration={ "BlockPublicAcls": True, "IgnorePublicAcls": True, "BlockPublicPolicy": True, "RestrictPublicBuckets": True})Using
git archive has a useful side effect: uncommitted changes, local .env files and virtual environments never get deployed. If it isn't committed, it isn't live.4. A boring, reliable instance
A few small choices do most of the work:
- The AMI is resolved from the SSM public parameter (
resolve:ssm:/aws/service/ami-amazon-linux-latest/...), so the script always launches the latest Amazon Linux 2023 without hard-coding an AMI ID per Region.
- IMDSv2 is required (
HttpTokens: "required"), which blocks a whole class of SSRF credential-theft attacks against the metadata endpoint.
- User data installs Python 3.11, pulls the bundle from S3, installs only the server requirements (no PyTorch on a small instance), and registers a systemd service with
Restart=always.
- The health check treats HTTP 401 as "up": the demo sits behind basic auth, so a 401 means the server is running and asking for the password.
5. Teardown that actually tears down
Because everything is tagged and named predictably,
--destroy walks the list in dependency order: terminate the instances and wait, delete the security group, detach and delete the role and instance profile, empty and delete the bucket, then delete the parameters. Missing resources are skipped, so you can run it twice safely.This matters most on student accounts. The resource you forget to delete is the one that shows up on your bill.
What I'd change for production
This is a demo pattern, so here's what it doesn't cover:
- No TLS. Port 80 with basic auth sends the password in plain text. For anything real, put an Application Load Balancer with an ACM certificate, or CloudFront, in front of it.
- The Bedrock statement uses
Resource: "*". Scope it to the specific models you call.
- One instance, no autoscaling. That's fine for judging traffic, and wrong for users.
- Default VPC and a public IP. A real setup would use private subnets and a load balancer.
- Imperative boto3 calls. For something long-lived, the same resources belong in AWS CDK or CloudFormation, so drift and rollbacks come for free.
For a hackathon, though, this gets you a live URL in about five minutes, with tightly scoped access and nothing left behind afterwards.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article