Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an Internet Gateway to a VPC using AWS CDK?

I am trying to produce the correct CDK scripts (TypeScript) to create an environment with a Lambda (accessed via API Gateway) that can call an RDS (Sql Server instance).

I believe I have it mostly working, but I wanted to connect to the RDS instance from my development machine to run some queries and check on a few things.

My RDS instance is in a private subnet, and so I believe in order to connect to it I need to add an Internet Gateway and security group to allow access on the appropriate ports.

For the life of me I can figure out the last piece, how to add the internet gateway using CDK.

The latest script I have tried is as follows:

const privateSubnectConfiguration = {
  cidrMask: 26,
  name: 'private-subnet',
  subnetType: SubnetType.PRIVATE,
};

const publicSubnectConfiguration = {
  cidrMask: 26,
  name: 'public-subnet',
  subnetType: SubnetType.PUBLIC,
};

const vpc = new Vpc(this, props.generateId('vpc'), {
  maxAzs: 2,
  subnetConfiguration: [privateSubnectConfiguration, publicSubnectConfiguration],
  natGateways: 1,
});

vpc.addGatewayEndpoint(props.generateId('internet-gateway'), {
  service: { name: "ig-service" }
})

Which then errors with The Vpc Endpoint Service 'ig-service' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidServiceName;

I see references to the CfnInternetGateway in the docs, but just can't figure out how to attach a new one to my VPC?

Can you please help with the syntax?

like image 526
shenku Avatar asked Nov 12 '19 05:11

shenku


People also ask

How to create a VPC in AWS CDK?

In order to create a VPC in AWS CDK, we have to instantiate and configure the Vpc class. Let's look at an example of creating a VPC in CDK: Copied! In the code snippet we used the Vpc class to create a VPC resource. The configuration props we passed to the construct are: cidr - the CIDR block of the VPC.

How to create an internet gateway using CDK VPC construct?

When using CDK VPC construct, an Internet Gateway is created by default whenever you create a public subnet. The default route is also setup for the public subnet. So you should remove addGatewayEndpoint () from your code, which adds a Gateway VPC Endpoint that you don't need.

How do I create an internet gateway in AWS?

You can create an internet gateway by navigating to VPC and creating it via the AWS web console: That's all it takes, providing it a name and internet gateway is created. So, we created an internet gateway, but it's just sitting there by itself. It's not doing anything yet.

What is an AWS VPC internet gateway?

An Internet Gateway is a logical connection between an AWS VPC and the Internet. It is not a physical device. Each VPC has only one Internet Gateway. If a VPC doesn’t have an Internet Gateway, then the resources cannot be accessed from the Internet. Conversely, resources within your VPC need an Internet Gateway to access the Internet.


Video Answer


1 Answers

First of all, you cannot directly access a database in a private subnet. You have to deploy a proxy instance in your public subnet and forward the required ports to access your database.

When using CDK VPC construct, an Internet Gateway is created by default whenever you create a public subnet. The default route is also setup for the public subnet. So you should remove addGatewayEndpoint() from your code, which adds a Gateway VPC Endpoint that you don't need.

You may also consider using SubnetType.ISOLATED to create a private subnet without a NAT GW, which may be redundant in your case. SubnetType.PRIVATE creates a NAT Gateway by default.

like image 119
Vikyol Avatar answered Sep 27 '22 23:09

Vikyol