Attach Elastic IP to EC2 instance with AWS CDK and Java

Hamza Sabljakovic
2 min readDec 11, 2022
Output of running the AWS cdk deploy command

We will begin by scaffolding a fresh Java AWS CDK project.

mkdir aws-linux-elastic-ip && cd aws-linux-elastic-ip  && cdk init app --language java

Next, in the stack, define the following resources, a new VPC, a security group with opened port 22, an EC2 instance running AWS Linux AMI, an elastic IP, and, an association between the EC2 instance and the elastic IP.

If you are looking for a bit more detailed post on how to manage EC2 instances with AWS CDK, check out my other post.

package com.myorg;

import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.services.ec2.*;
import software.constructs.Construct;


public class AwsLinuxElasticIpStack extends Stack {
public AwsLinuxElasticIpStack(final Construct scope, final String id) {
this(scope, id, null);
}

public AwsLinuxElasticIpStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);

final Vpc vpc = Vpc.Builder.create(this, id + "-vpc")
.vpcName(id + "-vpc")
.natGateways(0) // Do not create any NATs
.build();

final ISecurityGroup securityGroup = SecurityGroup.Builder.create(this, id + "-sg")
.securityGroupName(id)
.vpc(vpc)
.build();

securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(22));

final Instance ec2Instance = Instance.Builder.create(this, id + "-ec2")
.instanceName(id + "-ec2")
.machineImage(MachineImage.latestAmazonLinux())
.securityGroup(securityGroup)
.instanceType(InstanceType.of(
InstanceClass.BURSTABLE3,
InstanceSize.MICRO
))
.vpcSubnets(
SubnetSelection.builder()
.subnetType(SubnetType.PUBLIC)
.build()
)
.vpc(vpc)
.build();

final CfnEIP eip = new CfnEIP(this, id + "-eip");
final CfnEIPAssociation eipAssociation = new CfnEIPAssociation(
this,
id + "-eip-association",
CfnEIPAssociationProps.builder()
.eip(eip.getRef())
.instanceId(ec2Instance.getInstanceId())
.build()
);
}
}

You might be wondering why are resources related to Elastic IP prefixed with the “Cfn”. The Cfn prefix is not unique to Elastic IPs constructs. All the class names from the AWS CDK starting with the Cfn are level 1 (L1) constructs. Amazon CDK has three levels of constructs.

Starting from the lowest one, level 1 (L1), the code is automatically generated based on the Cloud formation specification.

Next, the level two (L2) constructs, where code is written by hand, usually u much more ergonomic interface with defaults configured based on AWS best practices (VPC and EC2 are examples of L2 constructs). Generally, the majority of the core services have the L2 constructs.

The third and final level of abstraction is Patterns (L3). Patterns are a collection of L1 and L2 constructs packaged together into a common AWS use case. A good example of a L3 constuct would be an ECS fargate cluster and an application load balancer.

The full source code is available on github

--

--