internal TheEfsLambdaStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // EFS needs to be setup in a VPC with 2Azs _vpc = new EC2.Vpc(this, "Vpc", new EC2.VpcProps { MaxAzs = 2 }); // Create a file system in EFS to store information _fileSystem = new EFS.FileSystem(this, "Filesystem", new EFS.FileSystemProps { Vpc = _vpc, RemovalPolicy = RemovalPolicy.DESTROY }); // Create a access point to EFS EFS.AccessPoint accessPoint; accessPoint = _fileSystem.AddAccessPoint("AccessPoint", new EFS.AccessPointOptions { CreateAcl = new EFS.Acl { OwnerGid = "1001", OwnerUid = "1001", Permissions = "750" }, Path = "/export/lambda", PosixUser = new EFS.PosixUser { Gid = "1001", Uid = "1001", } }); // Create the lambda function _functionProxyHandler = new Lambda.Function(this, "efsLambdaFunction", new Lambda.FunctionProps { Runtime = Lambda.Runtime.PYTHON_3_8, Code = Lambda.Code.FromAsset("lambda_fns"), Handler = "message_wall.lambda_handler", Vpc = _vpc, Filesystem = Lambda.FileSystem.FromEfsAccessPoint(accessPoint, "/mnt/msg") }); // Api Gateway HTTP integration _apiGateway = new APIGv2.HttpApi(this, "EFS Lambda", new APIGv2.HttpApiProps { DefaultIntegration = new APIGv2Integration.LambdaProxyIntegration(new APIGv2Integration.LambdaProxyIntegrationProps { Handler = _functionProxyHandler }) }); // Output to CFN new CfnOutput(this, "HTTP API Url", new CfnOutputProps { Value = _apiGateway.Url }); }
internal PollyStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Lambda Function that takes in text and returns a polly voice synthesis _pollyFunction = new Lambda.Function(this, "pollyHandler", new Lambda.FunctionProps { Runtime = Lambda.Runtime.PYTHON_3_8, Code = Lambda.Code.FromAsset("lambda_fns"), Handler = "polly.handler" }); // https://docs.aws.amazon.com/polly/latest/dg/api-permissions-reference.html // https://docs.aws.amazon.com/translate/latest/dg/translate-api-permissions-ref.html _pollyPolicy = new IAM.PolicyStatement(new IAM.PolicyStatementProps { Effect = IAM.Effect.ALLOW, Resources = new[] { "*" }, Actions = new[] { "translate:TranslateText", "polly:SynthesizeSpeech" } }); _pollyFunction.AddToRolePolicy(_pollyPolicy); // defines an API Gateway Http API resource backed by our "pollyHandler" function. _httpApi = new APIGatewayV2.HttpApi(this, "Polly", new APIGatewayV2.HttpApiProps { DefaultIntegration = new APIGatewayV2Integrations.LambdaProxyIntegration( new APIGatewayV2Integrations.LambdaProxyIntegrationProps { Handler = _pollyFunction }) }); new CfnOutput(this, "HTTP API Url", new CfnOutputProps { Value = _httpApi.Url }); }
internal TheStateMachineStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Step Function Starts Here // The first thing we need to do is see if they are asking for pineapple on a pizza _pineappleCheckHandler = new Lambda.Function(this, "pineappleCheckHandler", new Lambda.FunctionProps { Runtime = Lambda.Runtime.NODEJS_12_X, Code = Lambda.Code.FromAsset("lambda_fns"), Handler = "orderPizza.handler" }); /* * Step functions are built up of steps, we need to define our first step * This step was refactored due to Deprecated function */ _orderPizzaTask = new StepFunctionTasks.LambdaInvoke(this, "Order Pizza Job", new StepFunctionTasks.LambdaInvokeProps { LambdaFunction = _pineappleCheckHandler, InputPath = "$.flavour", ResultPath = "$.pineappleAnalysis", PayloadResponseOnly = true }); // Pizza Order failure step defined _jobFailed = new StepFunction.Fail(this, "Sorry, We Dont add Pineapple", new StepFunction.FailProps { Cause = "They asked for Pineapple", Error = "Failed To Make Pizza" }); // If they didnt ask for pineapple let's cook the pizza _cookPizza = new StepFunction.Pass(this, "Lets make your pizza"); // If they ask for a pizza with pineapple, fail. Otherwise cook the pizza _chainDefinition = StepFunction.Chain .Start(_orderPizzaTask) .Next(new StepFunction.Choice(this, "With Pineapple?") // Logical choice added to flow .When(StepFunction.Condition.BooleanEquals("$.pineappleAnalysis.containsPineapple", true), _jobFailed) .Otherwise(_cookPizza)); // Building the state machine _stateMachine = new StepFunction.StateMachine(this, "StateMachine", new StepFunction.StateMachineProps { Definition = _chainDefinition, Timeout = Duration.Minutes(5), TracingEnabled = true, StateMachineType = StepFunction.StateMachineType.EXPRESS }); /** * HTTP API Definition **/ // We need to give our HTTP API permission to invoke our step function _httpApiRole = new IAM.Role(this, "HttpAPIRole", new IAM.RoleProps { AssumedBy = new IAM.ServicePrincipal("apigateway.amazonaws.com"), InlinePolicies = new Dictionary <string, IAM.PolicyDocument> { { "AllowSFNExec", new IAM.PolicyDocument(new IAM.PolicyDocumentProps { Statements = new IAM.PolicyStatement[] { new IAM.PolicyStatement(new IAM.PolicyStatementProps { Actions = new string[] { "states:StartSyncExecution" }, Effect = IAM.Effect.ALLOW, Resources = new string[] { _stateMachine.StateMachineArn } }) } }) } } }); _api = new APIGateway.HttpApi(this, "TheStateMachineAPI", new APIGateway.HttpApiProps { CreateDefaultStage = true }); _integration = new APIGateway.CfnIntegration(this, "Integration", new APIGateway.CfnIntegrationProps { ApiId = _api.HttpApiId, IntegrationType = "AWS_PROXY", ConnectionType = "INTERNET", IntegrationSubtype = "StepFunctions-StartSyncExecution", CredentialsArn = _httpApiRole.RoleArn, RequestParameters = new Dictionary <string, string> { { "Input", "$request.body" }, { "StateMachineArn", _stateMachine.StateMachineArn } }, PayloadFormatVersion = "1.0", TimeoutInMillis = 10000 }); new APIGateway.CfnRoute(this, "DefaultRoute", new APIGateway.CfnRouteProps { ApiId = _api.HttpApiId, RouteKey = APIGateway.HttpRouteKey.DEFAULT.Key, Target = "integrations/" + _integration.Ref }); new Amazon.CDK.CfnOutput(this, "HTTP API Url", new Amazon.CDK.CfnOutputProps { Value = _api.Url }); }