Creating AWS API Gateway entry for a lambda in F#
Creating AWS API Gateway entry for a lambda in F#
ADD
aws-fs-lambda-webserver/LambdaWebServer/Readme.md
+# Giraffe Serverless Application
+
+This project shows how to run a [Giraffe](https://github.com/giraffe-fsharp/Giraffe) project as an AWS Lambda exposed through Amazon API Gateway. The NuGet package [Amazon.Lambda.AspNetCoreServer](https://www.nuget.org/packages/Amazon.Lambda.AspNetCoreServer) contains a Lambda function that is used to translate requests from API Gateway into the ASP.NET Core framework and then the responses from ASP.NET Core back to API Gateway.
+
+
+### Project Files ###
+
+* serverless.template - an AWS CloudFormation Serverless Application Model template file for declaring your Serverless functions and other AWS resources
+* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS
+* Setup.fs - Code file that contains the bootstrap for configuring ASP.NET Core and Giraffe. It contains a main function for local development and the LambdaEntryPoint type for executing from Lambda. The LambdaEntryPoint type inherits from **Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction** which contains the logic of converting requests and response back and forth between API Gateway and ASP.NET Core.
+* AppHandlers.fs - Code file that defines the HTTP handler functions and routing function.
+* web.config - used for local development.
+
+You may also have a test project depending on the options selected.

ADD
aws-fs-lambda-webserver/LambdaWebServer/Setup.fs
+module Setup
+
+open System
+open System.IO
+open Microsoft.AspNetCore.Builder
+open Microsoft.AspNetCore.Hosting
+open Microsoft.Extensions.Configuration
+open Microsoft.Extensions.Logging
+open Microsoft.Extensions.DependencyInjection
+open Giraffe
+
+
+
+// ---------------------------------
+// Config and Main
+// ---------------------------------
+
+
+let configureApp (app : IApplicationBuilder) =
+ let env = app.ApplicationServices.GetService<IHostingEnvironment>()
+ (match env.IsDevelopment() with
+ | true -> app.UseDeveloperExceptionPage()
+ | false -> app.UseGiraffeErrorHandler AppHandlers.errorHandler)
+ .UseStaticFiles()
+ .UseGiraffe(AppHandlers.webApp)
+
+let configureServices (services : IServiceCollection) =
+
+ // To add AWS services to the ASP.NET Core dependency injection add
+ // the AWSSDK.Extensions.NETCore.Setup NuGet package. Then
+ // use the "AddAWSService" method to add AWS service clients.
+ //
+ // services.AddAWSService<Amazon.S3.IAmazonS3>() |> ignore
+
+ services.AddGiraffe() |> ignore
+
+let configureLogging (builder : ILoggingBuilder) =
+ let filter (l : LogLevel) = l.Equals LogLevel.Error
+ builder.AddFilter(filter).AddConsole().AddDebug() |> ignore
+
+let configureAppConfiguration (ctx:WebHostBuilderContext) (builder : IConfigurationBuilder) =
+
+ builder.AddJsonFile("appsettings.json", true, true)
+ .AddJsonFile((sprintf "appsettings.%s.json" ctx.HostingEnvironment.EnvironmentName), true, true)
+ .AddEnvironmentVariables() |> ignore
+
+// ---------------------------------
+// This type is the entry point when running in Lambda. It has similar responsiblities
+// to the main entry point function that can be used for local development.
+// ---------------------------------
+type LambdaEntryPoint() =
+ inherit Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction()
+
+ override this.Init(builder : IWebHostBuilder) =
+ let contentRoot = Directory.GetCurrentDirectory()
+
+ builder
+ .UseContentRoot(contentRoot)
+ .Configure(Action<IApplicationBuilder> configureApp)
+ .ConfigureServices(configureServices)
+ |> ignore
+
+// ---------------------------------
+// The main function is used for local development.
+// ---------------------------------
+[<EntryPoint>]
+let main _ =
+ let contentRoot = Directory.GetCurrentDirectory()
+ let webRoot = Path.Combine(contentRoot, "WebRoot")
+ WebHostBuilder()
+ .UseKestrel()
+ .UseContentRoot(contentRoot)
+ .UseIISIntegration()
+ .UseWebRoot(webRoot)
+ .ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureAppConfiguration)
+ .Configure(Action<IApplicationBuilder> configureApp)
+ .ConfigureServices(configureServices)
+ .ConfigureLogging(configureLogging)
+ .Build()
+ .Run()
+ 0
\ No newline at end of file

ADD
aws-fs-lambda-webserver/README.md
+# AWS F# Lambda Web Server
+This example creates a web server in AWS lambda using the Giraffe web server
+
+## Deploying the App
+
+To deploy your infrastructure, follow the below steps.
+
+### Prerequisites
+
+1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
+2. [Configure AWS Credentials](https://www.pulumi.com/docs/intro/cloud-providers/aws/setup/)
+
+### Steps
+
+After cloning this repo, from this working directory, run these commands:
+
+1. Build and publish the lambda function, making the output available to our Pulumi program.
+
+```bash
+dotnet publish ./LambdaWebService
+```
+
+2. Execute our Pulumi program to archive our published function output, and create our lambda.
+```bash
+pulumi up -C ./pulumi
+```
+
+3. In a browser, navigate to the URL for `websiteUrl`. You should see the welcome message.

ADD
aws-fs-lambda-webserver/pulumi/Pulumi.yaml
+name: LambdaWebServer
+runtime: dotnet
+description: Basic example of an AWS serverless website using F#

Creating AWS API Gateway entry for a lambda in F# (#525)

Creating AWS API Gateway entry for a lambda in F#