Exemplo n.º 1
0
        internal InfraStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var stackProps = props as InfraStackProps;

            var imageTag = new CfnParameter(this, "ImageTag", new CfnParameterProps
            {
                Type        = "String",
                Default     = "latest",
                Description = "The tag of the image that needs to be deployed to the lambda"
            });

            var dockerImageCode = DockerImageCode.FromEcr(Repository.FromRepositoryName(
                                                              this,
                                                              stackProps.EcrRepoName,
                                                              stackProps.EcrRepoName
                                                              ), new EcrImageCodeProps {
                Tag = imageTag.ValueAsString
            });

            var lambda = new DockerImageFunction(this, "TimesheetApi", new DockerImageFunctionProps
            {
                FunctionName = "TimesheetApi",
                Code         = dockerImageCode,
                Description  = "Timesheet API",
                Timeout      = Duration.Seconds(10),
            });

            // DynamoDb Table
            var table = new Table(this, "Timesheet", new TableProps
            {
                TableName    = "Timesheet",
                PartitionKey = new Attribute
                {
                    Name = "Id",
                    Type = AttributeType.STRING
                },
                SortKey = new Attribute
                {
                    Name = "Day",
                    Type = AttributeType.STRING
                }
            });

            // Assign permission to lambda to access the Timesheet Table
            lambda.AddToRolePolicy(new PolicyStatement(new PolicyStatementProps
            {
                Actions   = new[] { "dynamodb:*" },
                Resources = new[] { table.TableArn }
            }));
        }
Exemplo n.º 2
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
            });
        }
Exemplo n.º 3
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
            });
        }