Example #1
0
        internal LambdaApiSolutionStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            DockerImageCode dockerImageCode =
                DockerImageCode.FromImageAsset("src/LambdaApiSolution.DockerFunction/src/LambdaApiSolution.DockerFunction");
            DockerImageFunction dockerImageFunction = new DockerImageFunction(this, "LambdaFunction",
                                                                              new DockerImageFunctionProps()
            {
                Code        = dockerImageCode,
                Description = ".NET 5 Docker Lambda function"
            });
            HttpApi httpApi = new HttpApi(this, "APIGatewayForLambda", new HttpApiProps()
            {
                ApiName            = "APIGatewayForLambda",
                CreateDefaultStage = true,
                CorsPreflight      = new CorsPreflightOptions()
                {
                    AllowMethods = new[] { HttpMethod.GET },
                    AllowOrigins = new[] { "*" },
                    MaxAge       = Duration.Days(10)
                }
            });
            LambdaProxyIntegration lambdaProxyIntegration = new LambdaProxyIntegration(new LambdaProxyIntegrationProps()
            {
                Handler = dockerImageFunction,
                PayloadFormatVersion = PayloadFormatVersion.VERSION_2_0
            });

            httpApi.AddRoutes(new AddRoutesOptions()
            {
                Path        = "/casing",
                Integration = lambdaProxyIntegration,
                Methods     = new[] { HttpMethod.POST }
            });
            string    guid   = Guid.NewGuid().ToString();
            CfnOutput apiUrl = new CfnOutput(this, "APIGatewayURLOutput", new CfnOutputProps()
            {
                ExportName = $"APIGatewayEndpointURL-{guid}",
                Value      = httpApi.ApiEndpoint
            });
        }
Example #2
0
        internal CdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            // Sample Json file content
            var sampleJson = new Dictionary <string, object> {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            // File Names for reuse
            var stringFile = "string-file.txt";
            var jsonFile   = "json-file.json";

            // S3 Bucket - for the files
            var fileshareBucket = new Bucket(this, "filebucketshare", new BucketProps
            {
                Versioned = true
            });

            // S3 Bucket Deployment for the sample files for read
            new BucketDeployment(this, "DeployFiles", new BucketDeploymentProps()
            {
                Sources = new[] {
                    Source.Data(stringFile, "This sample Text!!!"),
                    Source.JsonData(jsonFile, sampleJson)
                },
                DestinationBucket = fileshareBucket
            });


            // Docker file with multi-stage build
            DockerImageCode dockerImageCode = DockerImageCode.FromImageAsset("../lambda/src/lambda");

            // Lambda from Image
            DockerImageFunction dockerImageFunction = new DockerImageFunction(this,
                                                                              "container-image-lambda-function",
                                                                              new DockerImageFunctionProps()
            {
                Code        = dockerImageCode,
                Description = ".NET 6 Docker Lambda function",
                Environment = new Dictionary <string, string>
                {
                    { "BUCKET_NAME", fileshareBucket.BucketName },
                    { "QUERYSTRING_KEY", "key" }
                },
                Timeout = Duration.Minutes(1)
            });

            // Lambda Permission
            fileshareBucket.GrantReadWrite(dockerImageFunction);

            // APIGateway
            var apiGateway = new RestApi(this, "cdkApi", new RestApiProps()
            {
                RestApiName = "CdkApi",
                Description = "Lambda Backed API - Get SignedUrl"
            });

            // APIGateway - Root Method
            apiGateway.Root.AddMethod(
                "GET",
                new LambdaIntegration(dockerImageFunction, new LambdaIntegrationOptions
            {
                Proxy = true
            })
                );

            // CDK - Output
            new CfnOutput(this, "TextFile-Test-URL", new CfnOutputProps
            {
                Value = apiGateway.Url + "?key=" + stringFile
            });

            new CfnOutput(this, "JsonFile-Test-URL", new CfnOutputProps
            {
                Value = apiGateway.Url + "?key=" + jsonFile
            });
        }