Reputation: 746
I'm learning Lambda and Python with boto3. I have the AWS Lambda code below in point #1, it uses boto3 to stop some ECS service, I trigger this Lambda through an API Gateway url curl -X POST https://blablabla.execute-api.xx-west-1.amazonaws.com/test/
, and it works okay.
I know that everything I put well coded under the handler will work, but I cannot put under the handler all at once to stop/start various services, instead I want to add different pieces of code within the same Lambda to do things like: a) start ECS service, b) start RDS DB, c) stop RDS DB, something like I try to suggest in point #2 below.
I know how the boto3 code should look like, so I want to add under these functions that I listed in the point #2 below the boto3 parameters, then in API Gateway create a resource (or something) to link the functions within the Lambda, so with the same url I can trigger a specific function, see my example in point #3 below.
In API Gateway (resources) I can add only one single "PUT Method" (per resource), which is what I use to trigger my Lambda, I'm mentioning this because I was thinking to create separate Lambdas, then to add one resource PUT method per each one, so after I can add stages with different ending for the url, but it seems that I can't, maybe there is a way but I just don't know it.
Perhaps I'm wrong and I can put everything under the handler?
What are the options I have to accomplish this?.
1)
import json
import boto3
import pprint
region = 'xx-west-1'
cluster_name = "dummy-XX102020"
service_name = "some-test"
def lambda_handler(event, context):
ecs_client = boto3.client('ecs', region_name=region)
ecs_client.update_service(
cluster=cluster_name,
service=service_name,
desiredCount=0
)
print(ecs_client)
asg_client = boto3.client('application-autoscaling', region_name=region)
asg_client.register_scalable_target(
ServiceNamespace='ecs',
ResourceId='service' + '/' + cluster_name + '/' + service_name,
ScalableDimension='ecs:service:DesiredCount',
MinCapacity=0,
MaxCapacity=0,
)
print(asg_client)
response = {
"statusCode": 200,
"body": json.dumps('Executed successfully')
}
return response
def start_ecs(I don't know what to put here)
my code here
def start_rds(I don't know what to put here)
my code here
def stop_rds(I don't know what to put here)
my code here
curl -X POST https://blablabla.execute-api.xx-west-1.amazonaws.com/start_ecs/
curl -X POST https://blablabla.execute-api.xx-west-1.amazonaws.com/start_rds/
curl -X POST https://blablabla.execute-api.xx-west-1.amazonaws.com/stop_rds/
Upvotes: 0
Views: 686
Reputation: 320
I have used API Gateway stages to trigger different lambda functions. You can put lambda_function_name
(in my case lbfunc) as a variable in the stage and use this variable in the integration request Lambda Function ${stageVariables.lambda_function_name}
You can see about AWS API Gateway stages here: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-stages.html
With this you can have one rest-api with different stages triggering different lambda functions.
In my case I had it for production and development so it looks like this:
https://blablabla.execute-api.eu-west-1.amazonaws.com/prod-v1/
Upvotes: 1