Lesson 02Build

Launching an EC2 instance

One construct, two properties — pick the machine image and the instance type, no launch wizard required.

An instance type is a family-and-size pair — t3.micro, t3.small, m5.large — picking how much CPU, memory, and network throughput an instance gets. Same machine image, different instance type, and you've got the same software running on a bigger or smaller machine.

The CDK way

Instance takes instanceType and machineImage directly in its constructor — one line picks both. InstanceType.of(InstanceClass.T3, InstanceSize.MICRO) selects the size; MachineImage.fromSsmParameter(parameterName) points at Canonical's own published SSM parameter for Ubuntu Server 24.04 LTS, so the AMI ID resolves at deploy time to whatever the latest patched build of that release actually is — no hardcoded AMI ID to go stale. Instance, InstanceType, InstanceClass, InstanceSize, and MachineImage all come from the same module, aws-cdk-lib/aws-ec2 — already imported in the starter.

Your task

On the right, the starter construct launches an instance sized t2.nano — real, but too small for a web server. Change instanceType to InstanceType.of(InstanceClass.T3, InstanceSize.MICRO), then run the synthesizer.

Tip: the VPC above your instance is given — a small, public-subnets-only network with no NAT gateway to pay for. It isn't part of this lesson's task; later lessons build on top of it unchanged.

2 assertions · cdk-nag AwsSolutions ruleset

Not run yet

Edit the construct, then run synth & validate to check your work.

Validation · TypeScriptNot run · 2 pending
Assertion 1
Assertion 2