tcj123
tcj123

Reputation: 187

AWS Lambda different IP address for each Lambda

Is it possible to have each Lambda access the internet from a different IP address? In my testing, it appears the each time a Lambda is invoked it uses the same IP address to access our servers.

Our Lambdas do not have VPC access and are not NAT'd. I would think we would get different IP addresses with this setup but that doesn't seem to be the case.

I am wondering if it's possible that our because our volume is low we just always end up using the same container hence the same IP address? If so is there any way to prevent this?

Upvotes: 15

Views: 12871

Answers (3)

Armando Sepulveda
Armando Sepulveda

Reputation: 11

The best way to do it would be to redeploy the code each time. I use boto3's update_function_code.

Add the following code anywhere in the function, it will deploy it for further calls. Therefore, the next Lambda will run from a different IP address.

import boto3
client = boto3.client('lambda')

response = client.update_function_code(
    FunctionName='arn:aws:lambda...',
    S3Bucket='bucket-name',
    S3Key='function.zip',
    Publish=True
)

Then export the function to a zip file, rename it as "function.zip" and upload it to an S3 bucket that matches the S3Bucket variable.

Note: It takes a few seconds to deploy the code. I'd suggest you use SQS with a delay time to manage the requests.

Upvotes: 1

adamwlev
adamwlev

Reputation: 107

When you redeploy the lambda it seems to use a new container. So you can automate this deploy process using awscli to get a new ip on command.

Upvotes: 2

Michael - sqlbot
Michael - sqlbot

Reputation: 179124

I am wondering if it's possible that our because our volume is low we just always end up using the same container hence the same IP address?

Yes, that is exactly the reason. A container is only spawned if no containers are already available. After a few minutes of no further demand, excess/unneeded containers are destroyed.

If so is there any way to prevent this?

No, this behavior is by design.

Upvotes: 10

Related Questions