Search code examples
amazon-web-servicesaws-lambdaaws-api-gatewaynat

how to set up a job that starts/stops the NAT gateway when lambda function is runing


Currently I'm doing a project and created a database instance in aws rds. We bought some sensors to monitor the water quality of some outfalls and the sensor will upload the data to the vendor's server. And we could reqeust this incremental data from vendor's website API.

What I want to realize is to set a script run automatically to request incremental data from vendor's website and import it into my aws database.

So created a lambda function and set a cloud watch to make it run automatically one time every day. The lambda function is requesting incremental data from a vendor's server and loading it into our own db instance in aws. Currently the system works well.

However there is a problem I found when checking my AWS billings.

https://i.sstatic.net/eojBo.png

As you can see there is a cost for NAT Gateway. In order to let the lambda function get access to the public internet, I created it by following the tutorial in this article. But I didn't expect that the cost depends on the hours it runs.

So is there a way to set up a job that starts/stops the NAT gateway only when I need it? Since the lambda function just run one time per day and it just takes about 3 or 4 seconds, the cost would be much lower if I don't need to maintain the NAT Gateway for all the time.


Solution

  • You can't stop a NAT gateway. You can only delete it. If you do this, next time you want to access the internet, you have to provision new one, modify all route tables to match the new NAT. Obviously this process can be automated, but it requires a custom solution.

    But maybe, for very limited use of the internet, instead of NAT Gateway, you could use NAT instance. You could setup tiny NAT instance, and since its instance, you could stop it when not in use.