I have a Lambda function that has an exposed API Gateway endpoint, and I can get the URL for that via the AWS console. However, I would like to get that URL via API call. Neither the Lambda API documentation nor the API Gateway documentation seem to have that information (or perhaps I've missed it), so is this even possible in the first place?
I don't really understand the above answer (maybe it's outdated?).
The absolute easiest way:
Choose "API Gateway" under "Services" in AWS.
Click on your API.
Click on "Stages".
Choose the stage you want to use
Now you can see the entire URL very visible inside a blue box on the top with the heading "Invoke URL"
Your API Gateway endpoint URL doesn't get exposed via an API call. However, since the URL of the API follows a certain structure, you could get all the necessary pieces and create the URI within your code.
https://API-ID.execute-api.REGION.amazonaws.com/STAGE
You could use apigateway:rest-apis to get your API-ID and restapi:stages to get the stage corresponding identifier.
I'm not seeing a direct answer to the OP's question (get endpoint URL using API). Here's a snippet of Python code that I hope will guide the way, even if you're using other language bindings or the CLI. Note the difference in approach for getting the internal endpoint vs getting any associated custom domain endpoints.
import boto3
apigw = boto3.client('apigateway')
def get_rest_api_internal_endpoint(api_id, stage_name, region=None):
if region is None:
region = apigw.meta.region_name
return f"https://{api_id}.execute-api.{region}.amazonaws.com/{stage_name}"
def get_rest_api_public_endpoints(api_id, stage_name):
endpoints = []
for item in apigw.get_domain_names().get('items',[]):
domain_name = item['domainName']
for mapping in apigw.get_base_path_mappings(domainName=domain_name).get('items', []):
if mapping['restApiId'] == api_id and mapping['stage'] == stage_name:
path = mapping['basePath']
endpoint = f"https://{domain_name}"
if path != "(none)":
endpoint += path
endpoints.append(endpoint)
return endpoints
Go to you lambda main page -> Click on "Application" [Function Overview section] -> Resource section -> LambdaAPIDefinition -> Stages tab -> [Select required stage] -> [Invoke URL] You can see the api endpoint visible.
Following up on #larschanders comment, if you create the gateway using CloudFormation, the endpoint URL is surfaced as one of the stack outputs.
If you use CloudFormation you can get this with Python and Boto3:
import boto3
cloudformation = boto3.resource('cloudformation')
stack = cloudformation.Stack(name=stack_name)
api_url = next(
output['OutputValue'] for output in stack.outputs
if output['OutputKey'] == 'EndpointURL')
This is from a working example of a REST service using Chalice that I put on GitHub. Here's a link to the pertinent code, in context: aws-doc-sdk-examples.
If you know the function name, you can get the apigw endpoint by fetching the policy:
aws lambda get-policy --function-name <name> \
| jq -r '.Policy | fromjson | .Statement[0].Condition.ArnLike."AWS:SourceArn"'
THen it's just a matter of extracting the api_id and construct the endpoint like Jurgen suggested
A Script is the only answer as of now
But if someone wants to do purely on CLI using commands here is the long way (though I am a very beginner in AWS and I tried this during learning Lambda basics), maybe there is a much better way...
The format of Invoke-URL of the endpoint is something like this
https://YOUR-REST-API-ID.execute-api.REGION.amazonaws.com/STAGE/RESOURCE
// get YOUR-REST-API-ID e.g., 0w12zl28di
aws apigateway get-rest-apis | jq -r '.items[] | [.id, .name] | #tsv'
// get REGION using Lambda_function_name e.g., ap-east-1
aws lambda get-function-configuration --function-name YOUR_LAMBDA_FUNCTION_NAME | jq '.FunctionArn | split(":")[3]'
// get STAGE e.g., dev
aws apigateway get-stages --rest-api-id YOUR-REST-API-ID | jq -r '.item[] | [.stageName] | #tsv'
// get RESOURCE e.g., users
aws apigateway get-resources --rest-api-id YOUR-REST-API-ID | jq '.items[] | [.id, .path]'
// finally put your pieces in this way
https://0w12zl28di.execute-api.ap-east-1.amazonaws.com/dev/users
Note you have to install jq parser (if you don't have it already)
https://stedolan.github.io/jq/download/
I was using Linux on top of Windows, so I need to do it in this way
curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
To very precise Go to AWS Console search :
Lambda under services
Search the Function name which are looking for.
In Function overview, you will find API GATEWAY (Click on this)
Under API GATEWAY, click on Details (down arrow)
Under Details, you will find all the details like
API endpoint :
API type :
Authorization :
Method :
Resource path :
Stage :
Related
I have setup my python api service using djangorestframework and I am using drf_yasg for showing swagger docs for my api.
Here is glance of setup:
schema_view = get_schema_view(
openapi.Info(
title='My API',
default_version='v1',
description='rest service',
terms_of_service='',
contact=openapi.Contact(email='my#email'),
license=openapi.License(name='BSD License'),
),
public=False,
)
urlpatterns = [
path('pyapi/weather/', include('apps.weather.urls')),
re_path(r'^pyapi/swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
re_path(r'^pyapi/swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
re_path(r'^pyapi/redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]
Next I setup this api with amazon ec2 and stuff, and I am using Amazon API Gateway to access the api from containers.
Now the problem is when I try to access that using api gateway domain, it returns swagger JSON instead of HTML.
I tried several things like setting Content-Type mappings in method response and integration response but nothing works.
In my local machine it shows html as expected, so I am suspecting problem is in my gateway settings.
I highly appreciate if someone can help!
Ok I solved mystery!
After tons of tries and looking here and there, I found there was problem in API Gateway Request header setting.
Actually drf-yasg also kind of weird let me tell you why.
After setting up urls as I shown in first image, if you try to access http://localhost:8000/pyapi/swagger/ it shows UI perfectly.
At that time value of request header is Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Now same URL if you pass request header "Accept: application/json" then istead of showing html UI, it shows swagger JSON! wutt!!
That I found in Amazon API Gateways's test method's output. It was by default sending "Accept: application/json" and thats why I was always getting swagger.json in output. That was showstopper thing!
I changed it to Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 and now I could see UI perfectly!
I hope this will save time of many other people like me who are new to this kind of stuff!
As part of a AWS SAM template, I have a function with an HttpPost event trigger. Because I'm using the AWS SAM transform, I am not explicitly declaring the API Gateway that gets created to route this http post to trigger the function. Given that, is there any way to reference the generated URL endpoint, such as in a stack output or describe-stack-resources, so that I can programatically get the invocation URL for the function? I know I can get the endpoint by navigating to the stack in the console, finding the ApiGateway resource, and clicking around randomly until one of the pages shows it. But I'd like a method that my application code can reproduce.
Shortened template for reference:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
...
SendJobUpdateFunction:
Type: AWS::Serverless::Function
Properties:
...
Runtime: nodejs10.x
Events:
HttpPost:
Type: Api
Properties:
Path: '/jobs'
Method: post
...
I'm currently deploying using the sam CLI, which has I think a very similar syntax to aws cloudformtion.
According to the documentation and this previous question, you can get it with:
!Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}"
Where ${Stage} is your own parameter containing the deployed stage.
I am trying to create the grafana users using API's and this is what I tried.
curl -XPOST -H “Content-Type: application/json” -d ‘{“name”:“User”,“email”:“user#graf.com”,“login”:“user”,“password”:“password”}’ http://admin:admin#localhost:3000/api/admin/users
got this: [{“classification”:“DeserializationError”,“message”:“invalid character ‘\’’ looking for beginning of value”},{“fieldNames”:[“Password”],“classification”:“RequiredError”,“message”:“Required”}]
Can any one help me?
I followed the http://docs.grafana.org/http_api/user/ documentation and now I am able to create the users and able to use all grafana api's
Here is the answer for my question
import requests
url='http://admin:admin#localhost:3000/api/admin/users'
data='''{
"name":"tester",
"email":"test#graadff.com",
"login":"tester",
"password":"test#123"
}'''
headers={"Content-Type": 'application/json'}
response = requests.post(url, data=data,headers=headers)
print (response.text)
I have a Lambda function, attached to an API Gateway endpoint, that lists the items under an ID in DynamoDB.
I've built my function using the Serverless Framework. When I invoke the function locally (serverless invoke local ...) it works fine. When I test it manually on the AWS Lambda console, it works fine, but when I call it from the API Gateway, it doesn't work.
It's configured to use this payload: (What I've used for testing)
{
"requestContext": {
"identity": {
"cognitoIdentityId": "468648c5-b135-4075-910a-8a648d66e67d"
}
}
}
In my app, I use the aws-amplify package. This is how I call the endpoint:
data = await API.get('endpoint-name', '/endpoint', {
body: {
requestContext: {
cognitoIdentityId: '468648c5-b135-4075-910a-8a648d66e67d'
}
}
});
Here, I get a 403 error. If I call it without the extra data, i.e:
data = await API.get('endpoint-name', '/endpoint');
I get an empty list [ ], but no error. Does aws-amplify automatically populate the cognitoIdentityId field? I'm following the serverless-stack tutorial, and they kinda gloss over this.
I also have CloudWatch set up, and I cannot see anything funky there. Any ideas on how to fix / debug this issue?
Cheers!
Ah - I think I solved it!
I configured my endpoint to take parameters from the body of a GET request, however (I think) either aws-amplify or the API Gateway doesn't support a request body when performing a GET. I noticed this by manually testing the API Gateway (Which I hadn't done before), and it stated Request Body is not supported for GET methods.
So I think I'll have to pass my parameters in the URL itself.
I write the code for couchbase view. follow this https://blog.couchbase.com/understanding-grouplevel-view-queries-compound-keys/
const mapDate = `function(doc, meta) {
emit(dateToArray(doc.updatedAt), {
_id: meta.id,
_rev: meta.rev,
updatedAt: doc.updatedAt
});
}`
and when I call http://localhost:4984/{db}/_design/{ddoc}/_view/{view} I got an error
Error running map function: ReferenceError: dateToArray is not defined
error image
I use sync-gateway version 1.4
What should I do?
In order to be able to query views via the Sync Gateway, you should be creating it through the Sync Gateway REST interface. You cannot query for views directly created on Couchbase Server.
This link should provide more insights into creating and querying views via Sync Gateway
The blog post you're referring to is about Couchbase Server, not Sync Gateway. So, it looks like dateToArray is not a pre-defined function available on Sync Gateway.