AWS Amazon EC2 Endpoint Subnet Association
This page shows how to write Terraform and CloudFormation for Amazon EC2 Endpoint Subnet Association and write them securely.
aws_vpc_endpoint_subnet_association (Terraform)
The Endpoint Subnet Association in Amazon EC2 can be configured in Terraform with the resource name aws_vpc_endpoint_subnet_association
. The following sections describe 2 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_vpc_endpoint_subnet_association" "nlb_svc_private_172_18_10" {
vpc_endpoint_id = aws_vpc_endpoint.nlb_endpoint_svc.id
subnet_id = data.terraform_remote_state.e1_vpc1.outputs.private_172_18_10
}
resource "aws_vpc_endpoint_subnet_association" "nlb_svc_private_172_18_11" {
resource "aws_vpc_endpoint_subnet_association" "ssm" {
count = length(aws_subnet.private_subnets.*.id)
vpc_endpoint_id = aws_vpc_endpoint.ssm.id
subnet_id = aws_subnet.private_subnets.*.id[count.index]
Parameters
-
id
optional computed - string -
subnet_id
required - string -
vpc_endpoint_id
required - string -
timeouts
single block
Explanation in Terraform Registry
Provides a resource to create an association between a VPC endpoint and a subnet.
NOTE on VPC Endpoints and VPC Endpoint Subnet Associations: Terraform provides both a standalone VPC Endpoint Subnet Association (an association between a VPC endpoint and a single
subnet_id
) and a VPC Endpoint resource with asubnet_ids
attribute. Do not use the same subnet ID in both a VPC Endpoint resource and a VPC Endpoint Subnet Association resource. Doing so will cause a conflict of associations and will overwrite the association.
Tips: Best Practices for The Other AWS Amazon EC2 Resources
In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
aws_ebs_volume
Ensure to use a customer-managed key for EBS volume encryption
It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).
aws_instance
Ensure to avoid storing AWS access keys in user data
It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
AWS::EC2::VPCEndpoint (CloudFormation)
The VPCEndpoint in EC2 can be configured in CloudFormation with the resource name AWS::EC2::VPCEndpoint
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::EC2::VPCEndpoint
Properties:
RouteTableIds:
- !Ref PrivateRouteTable
ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
VpcId: !Ref VPC
## Type: AWS::EC2::VPCEndpoint
## Properties:
## VpcId: !ImportValue vpc4
## ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
## RouteTableIds:
## - !ImportValue rtbVpc4Dmz
Type: AWS::EC2::VPCEndpoint
Properties:
SecurityGroupIds:
- Fn::ImportValue:
!Sub ${SecurityStack}-SgEndpoint
ServiceName: !Join
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub com.amazonaws.${AWS::Region}.s3
VpcEndpointType: Gateway
VpcId: !Ref VPC
RouteTableIds:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
RouteTableIds:
- !Ref myRouteTable
VpcEndpointType: Gateway
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
"Type": "AWS::EC2::VPCEndpoint",
"Properties": {
"PrivateDnsEnabled": true,
"SecurityGroupIds": [
"sg-test"
],
"Type": "AWS::EC2::VPCEndpoint",
"Properties": {
"RouteTableIds": [
{
"Fn::ImportValue": {
"Fn::Sub": "ControlledARouteTable${VPCIdentifier}"
"Type": "AWS::EC2::VPCEndpoint",
"Properties": {
"RouteTableIds": [
{
"Fn::ImportValue": {
"Fn::Sub": "ControlledARouteTable${VPCIdentifier}"
"Type": "AWS::EC2::VPCEndpoint",
"Properties": {
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Type": "AWS::EC2::VPCEndpoint",
"Properties": {
"RouteTableIds": [
{
"Fn::ImportValue": {
"Fn::Sub": "ControlledARouteTable${VPCIdentifier}"
Parameters
-
PolicyDocument
optional - Json -
PrivateDnsEnabled
optional - Boolean -
RouteTableIds
optional - List -
SecurityGroupIds
optional - List -
ServiceName
required - String -
SubnetIds
optional - List -
VpcEndpointType
optional - String -
VpcId
required - String
Explanation in CloudFormation Registry
Specifies a VPC endpoint for a service. An endpoint enables you to create a private connection between your VPC and the service. The service may be provided by AWS, an AWS Marketplace Partner, or another AWS account. For more information, see VPC Endpoints in the AWS PrivateLink User Guide.
A
gateway
endpoint serves as a target for a route in your route table for traffic destined for the AWS service. You can specify an endpoint policy to attach to the endpoint, which controls access to the service from your VPC. You can also specify the VPC route tables that use the endpoint.For information about connectivity when you use a gateway endpoint to connect to an Amazon S3 bucket from an EC2 instance, see Why can’t I connect to an S3 bucket using a gateway VPC endpoint.
An
interface
endpoint is a network interface in your subnet that serves as an endpoint for communicating with the specified service. You can specify the subnets in which to create an endpoint, and the security groups to associate with the endpoint network interface.A
GatewayLoadBalancer
endpoint is a network interface in your subnet that serves an endpoint for communicating with a Gateway Load Balancer that you've configured as a VPC endpoint service.
Frequently asked questions
What is AWS Amazon EC2 Endpoint Subnet Association?
AWS Amazon EC2 Endpoint Subnet Association is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon EC2 Endpoint Subnet Association?
For Terraform, the kaysal/cloud-networking and lab-lamz4/13_Ghost_app source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the DaisukeMiyamoto/parallelcluster-closednetwork, applerom/gos and jun-suzuki1028/sample-templete source code examples are useful. See the CloudFormation Example section for further details.