How to get the auto generated RestApi from my AWS SAM template? To use in another SAM template - aws-cloudformation

I used AWS SAM to generate my Lambda/APIs. But I want to be able to get this RestApi so I can use it in another SAM template.
The idea is to have 1 base infra CloudFormation/SAM template that creates the network, ALB, API Gateway things
Then each "micro-service" will have its own SAM template and it will create API endpoints referencing this "root" RestApi by specifying the RestApiId attribute
Is this a correct approach? Wonder if when I deploy each service, will it remove the APIs for the other services?

You can access default auto generated RestApi as ServerlessRestApi. This is logical resource id for auto generated RestApi resource.
ServerlessRestApi access example in template.yaml is as follows.
Outputs:
ApiRootURL:
Description: API Root URL
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerlessRestApi.Stage}"
You can see ServerlessRestApi in the resource list of you CloudFormation stack. ServerlessRestApi is not documented, so it might be changed in the future version.

Related

serverless framework: configuring a pre-existing lambda authenticator for HTTP API Gateway routes in serverless.yml

I would like to add a preexisting lambda authenticator to the routes of a preexisting http api gateway using the serverless framework. I have followed the docs, whereby I have specified the authorizer details under provider.httpApi (lines 15 to 18), and I have referenced the authorizer on the route(s) below (line 27 & 28). Though I get the error message:
Cannot setup authorizers for externally configured HTTP API
What am I doing wrong here? It must be for a HTTP API gateway and not a REST API gateway as thats what the current infra is configured as. Thanks
It looks like you're using an externally configured HTTP API (I'm guessing from the id being set). In such a situation, you cannot configure authorizers in this manner, you can only do so when you're provisioning HTTP API as a part of your serverless service. What you can do there, is to setup a shared authorizer in a more manual way as described in docs here: https://www.serverless.com/framework/docs/providers/aws/events/http-api#shared-authorizer
I came across this post when researching how to use API gateway authorizers and serverless framework. I was terraforming the API gateway therefore needed to terraform the authorizer as well. When created, I stashed the authorizer ID in a parameter store entry. This is a 6 character alphanumeric value such as tw9qgj. I then referenced the parameter as follows:
custom:
authorizerId: ${ssm:api_gateway_authoriser}
Then added the following block to each API e.g.
- httpApi:
path: /protected
method: get
authorizer:
id: ${self:custom.authorizerId}

Specify a $default API Gateway stage via CloudFormation template?

I'm using AWS SAM.
From the docs, I see "Stage names can contain only alphanumeric characters, hyphens, and underscores, or be $default."
But when I try:
Resources:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: '$default'
The stack create fails with message:
Stage name only allows a-zA-Z0-9_
I just want to be able to use the API base URL without the StageName in the path. My API has only one stage.
Is there a way to do that without hooking up a custom domain to the API?
For HTTP API:
StageName is optional as HTTP API creates $default stage by default which can be accessed without any prefix and auto deployes.
If no name is specified, AWS SAM uses the
$default stage from API Gateway
For REST API
StageName is required. There is no auto deploy for Rest APIs. SAM Template behind the scenes it creates that stage and deploy code to that stage and it allows only a-zA-Z0-9_

How to modify the service endpoint of an Azure Pipeline via API?

I want to modify the service endpoint of a pipeline in Azure DevOps via API.
Example:
Pipeline "build-a-release" uses my personal service endpoint "hello1". But I want it to use existing service endpoint "my-companys-global-service-endpoint" instead.
The documentation https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines?view=azure-devops-rest-6.0 does not show any "update" function, but I can update the service endpoint of a pipeline in the Azure DevOps GUI.
How can I automate this process via a script and API?
To update a service endpoint, you should use Endpoints - Update Service Endpoint API:
PUT https://dev.azure.com/{organization}/_apis/serviceendpoint/endpoints/{endpointId}?api-version=6.0-preview.4
To update a pipeline defintion, you could use Definitions - Update api to update it. When you capture the network log, you could see there is connectedServiceId in properties of repository parameter. You could update the connectedServiceId to change the service connection.
From my test, something you need to notice:
Make sure the request headers include the following items:
Make sure you have the correct "revision" value (the latest revision) in the body.

Serverless Framework - Get API Gateway URL for use in tests

I'm using the Serverless framework, and I want to be able to reference my API Gateway URL in my acceptance tests.
My test environment is regularly destroyed and then recreated, so hardcoding a URL into the tests is not possible.
I can see there are ways to reference API Gateway as an AWS environment variable, but this doesnt help me to locally get the URL for my tests.
I was hoping that the cloudformation output would be referenced in the .serverless package, and accessible via json, but this doesnt seem to be the case.
Any idea how I can reference the API Gateway URL in my acceptance test files?
NOTE: These tests need to be run on AWS, not using a local server to mimic API Gateway
The serverless-plugin-test-helper plugin can help here. It will generate a YAML file containing all of the outputs of your stack. This includes a couple of standard ones - the S3 bucket that was used (ServerlessDeploymentBucketName) and the base service endpoint (ServiceEndpoint).
If you are using Node and have your tests in the same directory as the stack being tested then there's also a module to read this file. Otherwise, it's just standard YAML and you can use whatever tools are convenient.
Consider adding an APIGateway custom domain for your API. You can then use a known DNS name for your acceptance tests.
You will need to add an ApiGateway base path mapping, apigateway domain name, and a route53 recordset to the resources section of your serverless.yml.

AWS CLI Rename API Gateway model

I'm trying to do something I expected to be really simple.
I'd like to simply rename a model created on my API Gateway through the AWS CLI scripting tool (using powershell).
I have had a look at lots of documentation, including this aws document, with which I can do pretty much anything on the model, except rename it.
I expected it to be something like:
aws apigateway update-model --rest-api-id RESTID --model-name 'ModelName' --
patch-operations op=replace,path=/name,value='NewModelName' --region
AWSREGION
Which is almost identical to how to update a model description:
aws apigateway update-model --rest-api-id RESTID --model-name 'MDescription'
--patch-operations op=replace,path=/description,value='NewDescription'
--region ap-southeast-2
Is there a particular reason why we cannot rename these models, or have I just missed something?
API Gateway does not support renaming a model.
The model name is used as the unique identifier for the model and can be referenced from many different resources/methods within the API. This makes implementing a rename operation very difficult as an arbitrary number of references would also need to be updated at the same time.