public DotNetLambdaWithApiGetway(Stack scope, string id, DotNetLambdaWithApiGetwayProps props) : base(scope, id) { // domain and certificate have been created manually on AWS Console for security purposes var snsTopic = new Topic(this, "WorkSplitRequest", new TopicProps() { TopicName = "work-split-request", DisplayName = "Work Split Request" }); snsTopic.AddSubscription(new EmailSubscription("*****@*****.**", new EmailSubscriptionProps())); var queue = new Queue(this, "QueueProcessor", new QueueProps() { RetentionPeriod = Duration.Days(2) }); snsTopic.AddSubscription(new SqsSubscription(queue)); var dotnetWebApiLambda = new Function(this, "WebLambda", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = props.Code, Handler = "Web::Web.LambdaEntryPoint::FunctionHandlerAsync" }); dotnetWebApiLambda.AddEnvironment("Region", scope.Region); dotnetWebApiLambda.AddEnvironment("SnsTopic", snsTopic.TopicArn); snsTopic.GrantPublish(dotnetWebApiLambda); var fullDomain = $"{props.SiteSubDomain}.{props.DomainName}"; var apiGetway = new LambdaRestApi(this, fullDomain, new LambdaRestApiProps() { Handler = dotnetWebApiLambda }); var apiDomain = apiGetway.AddDomainName("customDomain", new DomainNameOptions() { DomainName = fullDomain, Certificate = props.Certificate, SecurityPolicy = SecurityPolicy.TLS_1_2, EndpointType = EndpointType.EDGE }); new ARecord(this, "ApiGateway-ARecord", new ARecordProps() { Zone = props.Zone, RecordName = fullDomain, Target = RecordTarget.FromAlias(new ApiGateway(apiGetway)), }); scope.Log($"ApiGateway Url {id}", apiGetway.Url); scope.Log($"ApiGateway public domain {id}", apiDomain.DomainNameAliasDomainName); }
internal SimpleWebServiceStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { var userTable = new Table(this, "questions", new TableProps { PartitionKey = new Attribute { Name = "id", Type = AttributeType.STRING } }); var api = new RestApi(this, "Simple Web service API", new RestApiProps { }); var handler = new Function(this, "SimpleServiceApiHandler", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/SimpleWebService/resources"), // path from CDK app root (foler that contains cdk.json) Handler = "LambdaService.handler" // LambdaService is the name of the source file, without the extension; // handler is the function within simpleWebService.cs that is called to invoke the Lambda function. }); // So Lambda function can access the table handler.AddEnvironment("TABLE_NAME", userTable.TableName); // So Lambda function can read/write to table userTable.GrantReadWriteData(handler); var apiHandler = new LambdaIntegration(handler); var user = api.Root.AddResource("user").AddResource("{id}"); user.AddMethod("GET", apiHandler); }
internal LambdaQuipToS3ExporterCdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { var s3Bucket = new Bucket(this, "bucket", new BucketProps { RemovalPolicy = RemovalPolicy.DESTROY, BlockPublicAccess = BlockPublicAccess.BLOCK_ALL }); var cloudFrontDistribution = new Distribution(this, "cloudfront", new DistributionProps() { DefaultBehavior = new BehaviorOptions() { Origin = new S3Origin(s3Bucket), CachePolicy = CachePolicy.CACHING_DISABLED, ViewerProtocolPolicy = ViewerProtocolPolicy.REDIRECT_TO_HTTPS }, DefaultRootObject = "index.html", ErrorResponses = new[] { new ErrorResponse() { HttpStatus = 403, Ttl = Duration.Seconds(0), ResponsePagePath = "/index.html", ResponseHttpStatus = 200 } } }); var function = new Function(this, "function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/LambdaQuipToS3Exporter/bin/Release/netcoreapp3.1/publish"), Handler = "LambdaQuipToS3Exporter::LambdaQuipToS3Exporter.Function::FunctionHandler", MemorySize = 256, RetryAttempts = 0, ReservedConcurrentExecutions = 1, Timeout = Duration.Seconds(60) }); function.AddEnvironment("S3BucketOutput", s3Bucket.BucketName); s3Bucket.GrantReadWrite(function); s3Bucket.GrantPut(function); s3Bucket.GrantDelete(function); var scheduleRule = new Rule(this, "schedule-rule", new RuleProps { Schedule = Schedule.Rate(Duration.Minutes(2)), Targets = new[] { new LambdaFunction(function) } }); }
internal ColdStartSimulatorStack(Construct scope, string id, StackProps props = null) : base(scope, id, props) { var coldStartSimulatorLambdaPolicy = new Amazon.CDK.AWS.IAM.PolicyStatement { Effect = Amazon.CDK.AWS.IAM.Effect.ALLOW }; coldStartSimulatorLambdaPolicy.AddResources("*"); coldStartSimulatorLambdaPolicy.AddActions("lambda:*"); coldStartSimulatorLambdaPolicy.AddActions("xray:*"); var coldStartSimulatorSetupFunction = new Function(this, "coldstartsimulator-setup", new FunctionProps { FunctionName = "coldstartsimulator-setup", Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("../lambdas/coldstartsimulator/bin/Release/netcoreapp3.1/publish"), Handler = "ColdStartSimulator::ColdStartSimulator.StepFunctionTasks::Setup", Timeout = Duration.Seconds(31), MemorySize = 512 }); var coldStartSimulatorInvokeLambdaFunction = new Function(this, "coldstartsimulator-invokelambda", new FunctionProps { FunctionName = "coldstartsimulator-invokelambda", Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("../lambdas/coldstartsimulator/bin/Release/netcoreapp3.1/publish"), Handler = "ColdStartSimulator::ColdStartSimulator.StepFunctionTasks::InvokeLambda", Timeout = Duration.Seconds(31), MemorySize = 512 }); coldStartSimulatorInvokeLambdaFunction.AddToRolePolicy(coldStartSimulatorLambdaPolicy); var coldStartSimulatorTouchLambdaFunction = new Function(this, "coldstartsimulator-touchlambda", new FunctionProps { FunctionName = "coldstartsimulator-touchlambda", Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("../lambdas/coldstartsimulator/bin/Release/netcoreapp3.1/publish"), Handler = "ColdStartSimulator::ColdStartSimulator.StepFunctionTasks::TouchLambda", Timeout = Duration.Seconds(31), MemorySize = 512 }); coldStartSimulatorTouchLambdaFunction.AddToRolePolicy(coldStartSimulatorLambdaPolicy); var metricS3Bucket = new Bucket(this, "metric-bucket", new BucketProps { BucketName = "coldstart-metric-bucket", }); var coldStartSimulatorCollectMetricsFunction = new Function(this, "coldstartsimulator-collectmetrics", new FunctionProps { FunctionName = "coldstartsimulator-collectmetrics", Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("../lambdas/coldstartsimulator/bin/Release/netcoreapp3.1/publish"), Handler = "ColdStartSimulator::ColdStartSimulator.StepFunctionTasks::CollectMetrics", Timeout = Duration.Seconds(31), MemorySize = 512 }); metricS3Bucket.GrantPut(coldStartSimulatorCollectMetricsFunction); coldStartSimulatorCollectMetricsFunction.AddEnvironment("MetricS3BucketName", metricS3Bucket.BucketName); coldStartSimulatorCollectMetricsFunction.AddToRolePolicy(coldStartSimulatorLambdaPolicy); var setup = new LambdaInvoke(this, "Setup", new LambdaInvokeProps { LambdaFunction = coldStartSimulatorSetupFunction, OutputPath = "$.Payload" }); var touch = new LambdaInvoke(this, "Touch", new LambdaInvokeProps { LambdaFunction = coldStartSimulatorTouchLambdaFunction, OutputPath = "$.Payload" }); var invoke = new LambdaInvoke(this, "Invoke", new LambdaInvokeProps { LambdaFunction = coldStartSimulatorInvokeLambdaFunction, OutputPath = "$.Payload" }); var collectMetrics = new LambdaInvoke(this, "Collect Metrics", new LambdaInvokeProps { LambdaFunction = coldStartSimulatorCollectMetricsFunction, OutputPath = "$.Payload" }); var wait3Seconds = new Wait(this, "Wait 3 seconds", new WaitProps { Time = WaitTime.Duration(Duration.Seconds(3)) }); var wait30Seconds = new Wait(this, "Wait 30 seconds", new WaitProps { Time = WaitTime.Duration(Duration.Seconds(30)) }); var invokeAgainChoice = new Choice(this, "Invoke again?"); invokeAgainChoice.When(Condition.BooleanEquals("$.Continue", true), touch); invokeAgainChoice.Otherwise(wait30Seconds); wait30Seconds .Next(collectMetrics); var definition = setup .Next(touch) .Next(wait3Seconds) .Next(invoke) .Next(invokeAgainChoice); new StateMachine(this, "ColdStartSimulatorStateMachine", new StateMachineProps { Definition = definition, Timeout = Duration.Minutes(10) }); }
internal StepFunctionDemoStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { Bucket stepFunctionDemoBucket = new Bucket(this, "StepFunctionDemoBucket", new BucketProps { Encryption = BucketEncryption.S3_MANAGED, RemovalPolicy = RemovalPolicy.RETAIN }); /* * //Step Function invoking Lambda function * Function invokeOddEvenStepFunction = new Function(this, "InvokeOddEvenStepFunction", new FunctionProps * { * Runtime = Runtime.DOTNET_CORE_3_1, * Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), * Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::InvokeOddEvenStepFunction", * Timeout = Duration.Minutes(5), * MemorySize = 512, * Description = "Lambda Function that invokes the Demo Step Function", * * }); */ //Function to calculate Odd or Even Function oddOrEvenFunction = new Function(this, "OddOrEvenFunction", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::OddOrEvenFunction", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Lambda Function that calculates odd or even", }); //Demo Lambda to perform Process 1 Function process1Function = new Function(this, "Process1Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process1Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs process1", }); //Demo Lambda to perform Process 2 Function process2Function = new Function(this, "Process2Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process2Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs process2", }); //Demo Lambda to perform Process 1 Function process11Function = new Function(this, "Process11Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process11Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs job process1", }); //Demo Lambda to perform Process 2 Function process12Function = new Function(this, "Process12Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process12Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs job process2", }); /* * //State Machines that operates on odd or even logic * //lambda Invoke that calculates odd or even * //var startStep = new LambdaInvoke(this, "Start", new LambdaInvokeProps * //{ * // LambdaFunction = oddOrEvenFunction.LatestVersion, * * //}); * * var oddEvenFunction = new Task(this, "OddEvenFunction", new TaskProps * { * Task = new InvokeFunction(oddOrEvenFunction.LatestVersion) * }); * * //lambda Invoke that does Process 1 * //var process1 = new LambdaInvoke(this, "Process1", new LambdaInvokeProps * //{ * // LambdaFunction = process1Function.LatestVersion, * //}); * * var process1 = new Task(this, "Process1", new TaskProps * { * Task = new InvokeFunction(process1Function.LatestVersion) * }); * * //lambda Invoke that does Process 2 * //var process2 = new LambdaInvoke(this, "Process2", new LambdaInvokeProps * //{ * // LambdaFunction = process2Function.LatestVersion, * //}); * * var process2 = new Task(this, "Process2", new TaskProps * { * Task = new InvokeFunction(process2Function.LatestVersion) * }); * * //Choice to go to Process 1 or Process 2 based on input number is odd or even. * var isEven = new Choice(this, "Is the number Even?"); * * var chain = Chain.Start(oddEvenFunction).Next( * isEven * .When(Condition.StringEquals("$.Result", "Even"), process1) * .When(Condition.StringEquals("$.Result", "Even"), process2) * ); * */ //State Machine /* * var stateMachine = new StateMachine(this, "DemoStepFunction", new StateMachineProps * { * StateMachineName = "Demo-State-Machine", * Timeout = Duration.Minutes(5), * Definition = chain * }); */ //All Policies // 1. Invoke function policies /* * invokeOddEvenStepFunction.Role?.AddManagedPolicy(ManagedPolicy.FromManagedPolicyArn(this, "InvokeLambdaPolicy", "arn:aws:iam::aws:policy/AWSLambdaFullAccess")); * var policyStatement = new PolicyStatement * { * Sid = "CanInvokeStepFunctions", * Effect = Effect.ALLOW * }; * policyStatement.AddActions(new[] { "states:StartExecution" }); * * invokeOddEvenStepFunction.AddToRolePolicy(policyStatement); * policyStatement.AddResources(stateMachine.StateMachineArn); * invokeOddEvenStepFunction.AddEnvironment(Functions.OddEvenStateMachineArnKey, stateMachine.StateMachineArn); */ process12Function.AddEnvironment(Functions.StepFunctionDemoBucketKey, stepFunctionDemoBucket.BucketName); stepFunctionDemoBucket.GrantReadWrite(process12Function); }
internal StepFunctionDemoStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { Bucket stepFunctionDemoBucket = new Bucket(this, "StepFunctionDemoBucket", new BucketProps { Encryption = BucketEncryption.S3_MANAGED, RemovalPolicy = RemovalPolicy.RETAIN }); Table stepFunctionDemoTable = new Table(this, "StepFunctionDemoTable", new TableProps { BillingMode = BillingMode.PROVISIONED, PartitionKey = new Amazon.CDK.AWS.DynamoDB.Attribute { Name = "Id", Type = AttributeType.STRING }, RemovalPolicy = RemovalPolicy.DESTROY }); //Step Function invoking Lambda function Function invokeOddEvenStepFunction = new Function(this, "InvokeOddEvenStepFunction", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::InvokeOddEvenStepFunction", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Lambda Function that invokes the Demo Step Function", }); //Function to calculate Odd or Even Function oddOrEvenFunction = new Function(this, "OddOrEvenFunction", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::OddOrEvenFunction", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Lambda Function that calculates odd or even", }); //Demo Lambda to perform Process 1 Function process1Function = new Function(this, "Process1Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process1Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs process1", }); Function processAFunction = new Function(this, "ProcessAFunction", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process1Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs process1", }); //Demo Lambda to perform Process 2 Function process2Function = new Function(this, "Process2Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process2Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs process2", }); //Demo Lambda to perform Process 1 Function process11Function = new Function(this, "Process11Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process11Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs job process1", }); //Demo Lambda to perform Process 2 Function process12Function = new Function(this, "Process12Function", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::Process12Function", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that runs job process2", }); Function taskTokenExecutorFunction = new Function(this, "TaskTokenExecutorFunction", new FunctionProps { Runtime = Runtime.DOTNET_CORE_3_1, Code = Code.FromAsset("src/Demo.Services.Lambda/bin/Release/netcoreapp3.1/Demo.Services.Lambda.zip"), Handler = "Demo.Services.Lambda::Demo.Services.Lambda.Functions::TaskTokenExecutor", Timeout = Duration.Minutes(5), MemorySize = 512, Description = "Demo Lambda Function that executes Task Token Step", Environment = new Dictionary <string, string>() { ["STEP_FUNCTION_DEMO_BUCKET"] = stepFunctionDemoBucket.BucketName } }); stepFunctionDemoBucket.GrantReadWrite(taskTokenExecutorFunction); var oddEvenFunction = new Task(this, "OddEvenFunction", new TaskProps { Task = new InvokeFunction(oddOrEvenFunction.LatestVersion) }); var process1 = new Task(this, "Process1", new TaskProps { Task = new InvokeFunction(process1Function.LatestVersion) }); var processA = new Task(this, "ProcessA", new TaskProps { Task = new InvokeFunction(processAFunction.LatestVersion) }); var process2 = new Task(this, "Process2", new TaskProps { Task = new InvokeFunction(process2Function.LatestVersion) }); var process11 = new Task(this, "Process11", new TaskProps { Task = new InvokeFunction(process11Function.LatestVersion), ResultPath = "$.Resolved" }); var process12 = new Task(this, "Process12", new TaskProps { Task = new InvokeFunction(process12Function.LatestVersion) }); var taskTokenExecutor = new Task(this, "TaskTokenExecutor", new TaskProps { Task = new RunLambdaTask(taskTokenExecutorFunction.LatestVersion, new RunLambdaTaskProps() { IntegrationPattern = ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN, Payload = TaskInput.FromContextAt("$$.Task.Token") }), Parameters = new Dictionary <string, object> { ["Payload"] = new Dictionary <string, object> { ["TaskToken.$"] = "$$.Task.Token", ["State.$"] = "$" } } }); //Choice to go to Process 1 or Process 2 based on input number is odd or even. var isEven = new Choice(this, "Is the number Even?"); var isResolvedOrOverriden = new Choice(this, "Is Resolved Or Overriden?"); //var chain1 = Chain.Start(oddEvenFunction) // .Next(isEven // .When( // Condition.StringEquals("$.Result", "Even"), // Chain.Start(process1) // .Next(process11) // .Next(isResolvedOrOverriden // .When( // Condition.Or( // new[] // { // Condition.BooleanEquals("$.Resolved", true), // Condition.BooleanEquals("$.Override", true) // }), process12) // .Otherwise(process2))) // .When(Condition.StringEquals("$.Result", "Odd"), process2)); var chain1 = Chain.Start(oddEvenFunction) .Next(isEven .When( Condition.StringEquals("$.Result", "Even"), Chain.Start(process1) .Next(taskTokenExecutor) .Next(isResolvedOrOverriden .When( Condition.Or( new[] { Condition.BooleanEquals("$.Resolved", true), Condition.BooleanEquals("$.Override", true) }), process12) .Otherwise(process2))) .When(Condition.StringEquals("$.Result", "Odd"), process2)); //State Machine var stateMachine = new StateMachine(this, "JobDemoStateMachine", new StateMachineProps { StateMachineName = "JobDemoStateMachine", Timeout = Duration.Minutes(5), Definition = chain1 }); stateMachine.Role?.AddManagedPolicy(ManagedPolicy.FromManagedPolicyArn(this, "DynamoDBFullAccessForStepFunction", "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess")); stateMachine.Role?.AddManagedPolicy(ManagedPolicy.FromManagedPolicyArn(this, "LambdaFullAccessForStepFunction", "arn:aws:iam::aws:policy/AWSLambdaFullAccess")); var demofargateTask1 = new FargateTaskDefinition(this, "demoECSTask1Definition", new FargateTaskDefinitionProps { MemoryLimitMiB = 4096, Cpu = 2048 }); var demofargateTask2 = new FargateTaskDefinition(this, "demoECSTask2Definition", new FargateTaskDefinitionProps { MemoryLimitMiB = 4096, Cpu = 2048 }); stepFunctionDemoBucket.GrantReadWrite(demofargateTask2.TaskRole); IVpc publicVpc = Vpc.FromLookup(this, "PublicVPC", new VpcLookupOptions { Tags = new Dictionary <string, string> { ["Paces:VpcType"] = "Public" } }); var cluster = Cluster.FromClusterAttributes(this, "PublicCluster", new ClusterAttributes { ClusterName = "OHC-PACES", Vpc = publicVpc, SecurityGroups = new[] { SecurityGroup.FromSecurityGroupId(this, "SecurityGroup", "sg-0a1bab8166d8fb715") } }); var container1 = demofargateTask1.AddContainer("app", new ContainerDefinitionOptions { Image = ContainerImage.FromAsset(".", new AssetImageProps { File = "Dockerfile" }), Logging = LogDriver.AwsLogs(new AwsLogDriverProps { LogGroup = new LogGroup(this, "demoECSTask1LogGroup", new LogGroupProps { LogGroupName = "/ecs/demoECSTask1-" + RandomString.Generate(10, StackId), }), StreamPrefix = "logs" }), }); var container2 = demofargateTask2.AddContainer("app", new ContainerDefinitionOptions { Image = ContainerImage.FromAsset(".", new AssetImageProps { File = "Dockerfile.1" }), Environment = new Dictionary <string, string> { ["STEP_FUNCTION_DEMO_BUCKET"] = stepFunctionDemoBucket.BucketName }, Logging = LogDriver.AwsLogs(new AwsLogDriverProps { LogGroup = new LogGroup(this, "demoECSTask2LogGroup", new LogGroupProps { LogGroupName = $"/ecs/demoECSTask2-{RandomString.Generate(10, StackId)}", }), StreamPrefix = "logs" }) }); Rule rule = new Rule(this, "DemoJobRule", new RuleProps { Schedule = Schedule.Cron(new CronOptions { Day = "*", Hour = "*", Minute = "1", Month = "*", Year = "*" }), Description = "Runs demo job fargate task", Targets = new IRuleTarget[] { new EcsTask( new EcsTaskProps { Cluster = cluster, TaskDefinition = demofargateTask2, SubnetSelection = new SubnetSelection { OnePerAz = true } }) } }); //var ecsTask1 = new Task(this, "ecsTask1", new TaskProps //{ // InputPath = "$", // Task = new CustomTask(new RunEcsFargateTaskProps // { // Cluster = Cluster.FromClusterAttributes(this, "PublicCluster", new ClusterAttributes // { // ClusterName = "OHC-PACES", // Vpc = publicVpc, // SecurityGroups = new[] { SecurityGroup.FromSecurityGroupId(this, "SecurityGroup", "sg-0a1bab8166d8fb715") } // }), // TaskDefinition = fargateTask1, // ContainerOverrides = new[] // { // new ContainerOverride // { // ContainerDefinition = container, // Command = new []{"$.Data"} // }, // } // }) //}); var ecsTask1 = new Task(this, "EcsTask1", new TaskProps { InputPath = "$", Task = new RunEcsFargateTask(new RunEcsFargateTaskProps { Cluster = cluster, TaskDefinition = demofargateTask1, //ContainerOverrides = new[] //{ // new ContainerOverride // { // ContainerDefinition = container, // // }, //} }), Parameters = new Dictionary <string, object> { ["Overrides"] = new Dictionary <string, object> { ["ContainerOverrides"] = new Dictionary <string, string>[] { new Dictionary <string, string> { ["Name"] = "app", ["Command.$"] = "$.ECSPayload" } } } } }); var chain2 = Chain.Start(processA).Next(ecsTask1); var stateMachineWithTask = new StateMachine(this, "JobDemoStateMachine-1", new StateMachineProps { StateMachineName = "JobDemoStateMachine-1", Timeout = Duration.Minutes(15), Definition = chain2, Role = Role.FromRoleArn(this, "StateMachineWithTaskRole", "arn:aws:iam::342600918501:role/service-role/PacesEdi274DefaultStateMachineRole") }); //All Policies // 1. Invoke function policies invokeOddEvenStepFunction.Role?.AddManagedPolicy(ManagedPolicy.FromManagedPolicyArn(this, "InvokeLambdaPolicy", "arn:aws:iam::aws:policy/AWSLambdaFullAccess")); var policyStatement = new PolicyStatement { Sid = "CanInvokeStepFunctions", Effect = Effect.ALLOW }; policyStatement.AddActions(new[] { "states:StartExecution" }); invokeOddEvenStepFunction.AddToRolePolicy(policyStatement); policyStatement.AddResources(stateMachine.StateMachineArn); invokeOddEvenStepFunction.AddEnvironment(Functions.StateMachineArnKey, stateMachine.StateMachineArn); process12Function.AddEnvironment(Functions.StepFunctionDemoBucketKey, stepFunctionDemoBucket.BucketName); stepFunctionDemoBucket.GrantReadWrite(process12Function); var policyStatementDemofargateTask2 = new PolicyStatement { Sid = "CanNotifyStepFunction", Effect = Effect.ALLOW }; policyStatementDemofargateTask2.AddActions(new[] { "states:SendTask*" }); demofargateTask2.AddToExecutionRolePolicy(policyStatementDemofargateTask2); demofargateTask2.AddToTaskRolePolicy(policyStatementDemofargateTask2); policyStatementDemofargateTask2.AddAllResources(); }