Access EC2 Instance Service Via AWS Lambda Function - amazon-web-services

I have a EC2 Instance which only have Private IP and I have installed the Apache Kafka on same Instance running on say 10.0.4.44:9092.
Now, I have created the AWS Lambda Function which read the document from given Bucket and sent the Document body to Apache Kafka running on EC2 Instance.
Now, AWS Lambda is unable to access the EC2 Instance Service.
How Can I grant access to Lambda for accessing Apache kafka Service on EC2?

You need to add the Lambda function to the VPC the EC2 server is running in. Here's the announcement blog post of Lambda VPC support with a walk through for setting it up. Here's the official documentation.
A Lambda function in a VPC will not get a public IP address. This means it won't be able to access anything that is outside the VPC. Since you also need to access S3 you will need to setup an S3 VPC Endpoint so that your function will still have S3 access.
If your function needs access to other things outside the VPC then you will have to add it to a private subnet of the VPC with a NAT Gateway attached.

Related

How can I connect an AWS lambda inside a VPC to connect to a cloudformation stack?

current situation:
I'm developing an AWS lambda that would launch an EC2 instance through a cloud formation stack.
I've deployed it inside a VPC, and thus had created endpoints to give it access to ressources such as S3/DynamoDB. However I cannot find any endpoints for the cloud formation, and as a result my function gets stucked at:
Starting new HTTPS connection (1): cloudformation.ap-south-1.amazonaws.com:443
update 1
Here is the snippet of code I'm using to connect to cloudformation:
self.cfn = session.resource('cloudformation')
stackdata = self.cfn.create_stack(
StackName="STACK-{}".format(instance_name),
DisableRollback=True,
TemplateURL=constants.TEMPLATE_TYPE[instance_type],
Parameters=params,
Capabilities=['CAPABILITY_IAM', 'CAPABILITY_AUTO_EXPAND','CAPABILITY_NAMED_IAM']
)
Please be noted that my code works just fine in a none-VPC setup (if I deploy my lambda outside of a VPC)
Could anyone help me try to figure out what I'm missing here?
Lambda function that is deployed to the VPC doesn't have access to the internet. That means that it's not able to access any of the AWS services endpoints unless you do one of two things:
create a VPC endpoint for that service
Add NAT Gateway so Lambda function can use it to access internet
You add NAT gateway to the public subnet.
After that, you need to edit route tables for private subnets to point to the NAT gateway. When you add a Lambda function to the VPC, you choose in which subnets it can be deployed. It's necessary to associate all of those subnets with the NAT gateway, so you're sure that the Lambda function will always have access to the NAT gateway.
If your Lambda function really needs to be in VPC (it needs access to some other resources inside of VPC), this is ok, but if it's not really necessary, I'd suggest you just move it outside of VPC (NAT gateway is $35/month + traffic).
You can see the details here as well: AWS Knowledgebase

Why can my lambda function not access S3 and SQS when an EC2 instance in the same VPC can?

I have the following resources provisioned in AWS:
A VPC (the default VPC for my AWS region) with 3 subnets and an internet gateway
An EC2 instance in the VPC with an elastic IP attached, and a NodeJS application server running
A RDS instance in the VPC
A Lambda function configured to run in the VPC (because it needs to access RDS)
An S3 bucket
An SQS queue
The application server running on my EC2 instance is able to connect to S3 and SQS using the AWS SDK for NodeJS. All I had to do was specify the S3 bucket's name and SQS queue's url.
However, my lambda function was unable to do the same until I set up a VPC Gateway Endpoint for S3, and a VPC Interface Endpoint for SQS. This despite the lambda function having internet access - I was able to retrieve a file on the internet in a test run of the lambda function.
What was preventing the lambda function from accessing S3 and SQS until the VPC endpoints were created?
default VPC for my AWS region
The default VPC has all its subnets public. Lambda does not have internet access, even if you place it in such a subnet. Thus it can't access S3 nor anything else.
To enable internet access for your lambda, it must be placed in private subnet and use NAT to access the internet, as explained in AWS docs.
Alternatively, you have to create VPC interface endpoints for S3 and SQS. This way your lambda will use the VPC endpoints to access these services, rather then trying to do it using internet.

How to add a step to emr cluster within a vpc through lambda function which is inside a same vpc and private subnet?

when the lambda function is not within vpc then iam able to add a step to emr cluster but if the lambda function is residing inside vpc where emr cluster is present and same private VPC subnet also.
This time iam getting timeout error when iam trying to add a step in emr cluster using boto3 client module "add_job_flow_steps"
"errorMessage": "2020-05-14T02:48:46.771Z ad979ac2-ff26-476a-b301-23797caeeaa9 Task timed out after 123.10 seconds".
Do i need to add a VPC Endpoint for me to communicate between AWS services within same VPC Subnet or is there any other way which i could communicate?
when the lambda function is not within vpc then iam able to add a step to emr cluster
This works because lambda not in vpc, can access internet. Subsequently, you can connect to public endpoint of AWS services, such as EMR.
if the lambda function is residing inside vpc where emr cluster is present and same private VPC subnet also.
This does not work, because lambda in VPC does not have internet access:
If your function needs internet access, use NAT. Connecting a function to a public subnet does not give it internet access or a public IP address.
To enable your lambda access the EMR service you need to use either NAT gateway or VPC interface endpoint as shown in the following link:
Connect to Amazon EMR Using an Interface VPC Endpoint
Please not that lambda in VPC also requires modified execution role.

AWS : Accessing a REST API deployed on EC2 instance from a lambda function ( lambda inside a VPC)

I have deployed an application which exposes a REST API on EC2 instance.
This REST API needs to be accessible only from the lambda function.
I have created a VPC using launch VPC wizard and the VPC with a Single Public Subnet option.
I have created two subnets and a security group.
In the security group, I have configured the http port on which the REST service is running, to be accessible from the same security group. This security group is also assigned to Lambda function.
I have assigned the VPC, subnets and the security group to the lambda function.
I am following the below link -
Access EC2 Instance Service Via AWS Lambda Function
The lambda function is not able to connect to the REST API endpoint on the EC2 instance.
Any pointers?
Your associated VPC must have a NAT Gateway (or a NAT Instance) in a public subnet. This looks to be missing in your case (based on the data that you have shared in the question).
I would implore you to go through this official tutorial. It clearly demonstrates what you need to access public endpoints from a Lambda function that is placed inside a manually created VPC.

AWS Lambda Function Timeout on Connecting To RDS Database through RDS Proxy

I'm trying to test AWS RDS proxy so I created a lambda function and done all steps that are present in this official link
https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda
store RDS credentials in Secret Manager
create new role and also add Trust Policy
in lambda function, from the AWS console, add proxy and its status is available.
When I execute the lambda function, it times out with no errors it seems like the error might be on connecting to db with rds proxy because when I run the lambda function again without proxy, it works just fine.
I initially thought that it might be a security group issue, so I edit the security group of RDS Proxy and update inbound and allow 0.0.0.0 (outbound was already 0.0.0.0).
I used defaut VPC in RDS Database and RDS Proxy. The endpoint of RDS database is public.
Since RDS proxy is not available outside the VPC. Configure your lambda function to run inside the VPC. The following link will help:
https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html
Late answer.. thought these might help others.
You have to keep your lambdas inside the same VPC and subnets to access RDS proxy.
In any case if you want to access third party web api from your lambda, you have make the lambda subnets private (no Internet Gateway in route table) and assign a NAT gateway which is tied with a public subnet.
If you are accessing other AWS services which are out of VPC like S3, Secret Manager etc. then you have to create VPC endpoints for those services in your VPC.