Aws-lambda-dotnet: Swagger docs expose when integrating with api gateway

Created on 30 Jan 2018  路  22Comments  路  Source: aws/aws-lambda-dotnet

Hi guys,

I have a .net core web api which has its own swagger integration docs exposed via "swagger/v1/swagger.json" endpoint. It's deployed as lambda. I also have set up a api gateway which integrates with this lambda to expose it's all apis.

The problem I have is I can't access the swagger docs via the /swagger url. It always returns "message: forbibbden". I can definitely access the swagger if I run the web api locally.

Any ideas?

Thanks

Jason

guidance

Most helpful comment

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

All 22 comments

Can you create a simple repo repository for me to take a look at?

I am having the same issue.

Did you make any progress with this @jasondaicoder?

Same problem, please update if you find a fix. I am able to access the json directly, e.g. .. https://...../Prod/swagger/v1/swagger.json . Since API Gateway supports swagger, was thinking something magic about the "swagger" path. But, using the info from this page, http://eatcodelive.com/2017/05/19/change-default-swagger-route-in-an-asp-net-core-web-api/ , I changed it to "swag" and still had the same problem.

Interesting, when you access https://..../Prod/swagger it redirects you to https://.../swagger, basically removing the stage part of the path and back to the root, which then results in the message forbidden.

Not really guys. I thought it could be because api gateway is supposed to act for web api only but not for UI related. I ended up with hosting the UI somewhere else but pointing to the swagger json file of the project through api gateway. It works.

Thanks. Hmm, kinda pain to have to do this :) I do have a regular MVC home controller and Index page as part of my deploy and it works fine. Yep, I was just reading on a post where a person thought may be related to where the response was not json, but my Index page works fine.

With my solution you actually only need to deploy the UI once. As long as you load the json file from your project you don鈥檛 need to do anything anymore. We host the UI files on S3. Maybe AWS team can fix the issue later.

interesting.. you can deploy a .net core ui on S3 without a running kestrel? figured the swagger was kinda a runtime thing and needed to support the running dlls etc, e.g. not just a static set of files

@normj, the project is just a normal c# weapi with Lamdba entry point. Nothing really special. I will try to create a sample repository on GitHub later. I don鈥檛 think anything wrong with my project to be honest as other people are having the same issue. I think it could be some conflicts with api gateway builtin swagger docs.

@rslangham no you don鈥檛 need to deploy your project. All you need is to download the swagger ui static files which you can get from swagger site ( maybe a link to GitHub which you can find on swagger site).

@jasondaicoder thanks for the suggestion on using the swagger ui static files. I did this using a different implementation and it worked. I downloaded the swagger ui files and put them under my project wwwroot/swaggerui. In the Startup.cs Configure(..) I enabled static files access, e.g. app.UseStaticFiles. I modified the swagger ui index.html embedded javascript to use window.location.href to dynamically change the url to the correct path for the swagger json, this allows it to be deployed anywhere and work.

Really would like to find out the correct solution though :) Thanks again.

Hi there, I'm having the exact same issue, I hope it's resolved soon.

Observing the same issue as well.

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

@AnandKhedkar that worked for me thanks

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

That does show up the Index.html page. However, it doesn't allow execution of the service through the swagger interface for me.
The path in the Request URL for a GET call excludes Prod again.
I am however, using the c.RoutePrefix = string.Empty; to render the page at the root instead of adding /swagger.

Is there a property that could be set so that this will stay consistent across all the relative url references?

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

That does show up the Index.html page. However, it doesn't allow execution of the service through the swagger interface for me.
The path in the Request URL for a GET call excludes Prod again.
I am however, using the c.RoutePrefix = string.Empty; to render the page at the root instead of adding /swagger.

Is there a property that could be set so that this will stay consistent across all the relative url references?

Resolved with this solution Works perfectly and even shows the base path in the header of the swagger page.

Closing as it looks like the solution has been found.

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.
In which file to define this.

I was also having the same problem. Got solution-
Please make sure you have added the stage in a name in startup file as shown below. where Prod is my stage name.

app.UseSwaggerUI(c =>
              {
                  c.SwaggerEndpoint("/Prod/swagger/v1/swagger.json", "GST Console API - V1");

              });

Requesting @normj to close this.

this worked for me.
Thanks

I ran into the same issue; but HARDCODING PROD (the stage name) into the code is NOT AN OPTION Is there any way to identify what API gateway deployment stage a lambda is running under? a AWS environment var or something?

@grahamehorner Here what I did to resolve the problem without hardcoding. It worked on my local machine and on Lambda

app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("v1/swagger.json", "GST Console API - V1");
});

Basically just setup the endpoint with c.SwaggerEndpoint() using relative path url without "swagger" and leading "/".

If we supply relative path url without leading "/" to c.SwaggerEndpoint(), the absolute endpoint url will come from

AWS Serverless Url + "/" + c.RoutePrefix + "/" + the supplied url

where c.RoutePrefix is set to 'swagger' by default

For example, if your AWS Serverless Url is 'https://xyz.central.amazonaws.com/Prod', your absolute url will be 'https://xyz.central.amazonaws.com/Prod/swagger/v1/swagger.json'

Was this page helpful?
0 / 5 - 0 ratings