|
| 1 | +# Copyright 2016-2022, Pulumi Corporation. All rights reserved. |
| 2 | + |
| 3 | +import json |
| 4 | +import pulumi |
| 5 | +import pulumi_aws as aws |
| 6 | + |
| 7 | +# Create an HTTP API. |
| 8 | +api = aws.apigatewayv2.Api("example", |
| 9 | + protocol_type="HTTP" |
| 10 | +) |
| 11 | + |
| 12 | +# Create a stage and set it to deploy automatically. |
| 13 | +stage = aws.apigatewayv2.Stage("stage", |
| 14 | + api_id=api.id, |
| 15 | + name=pulumi.get_stack(), |
| 16 | + auto_deploy=True |
| 17 | +) |
| 18 | + |
| 19 | +# Create an event bus. |
| 20 | +bus = aws.cloudwatch.EventBus("bus") |
| 21 | + |
| 22 | +# Create an event rule to watch for events. |
| 23 | +rule = aws.cloudwatch.EventRule("rule", |
| 24 | + event_bus_name=bus.name, |
| 25 | + event_pattern=json.dumps({"source": ["my-event-source"]}) |
| 26 | +) |
| 27 | + |
| 28 | +# Define a policy granting API Gateway permission to publish to EventBridge. |
| 29 | +api_gateway_role = aws.iam.Role("api-gateway-role", |
| 30 | + assume_role_policy=json.dumps({ |
| 31 | + "Version": "2012-10-17", |
| 32 | + "Statement": [ |
| 33 | + { |
| 34 | + "Action": "sts:AssumeRole", |
| 35 | + "Effect": "Allow", |
| 36 | + "Principal": { |
| 37 | + "Service": "apigateway.amazonaws.com", |
| 38 | + }, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }), |
| 42 | + managed_policy_arns=[ |
| 43 | + "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", |
| 44 | + ], |
| 45 | +) |
| 46 | + |
| 47 | +# Create an API Gateway integration to forward requests to EventBridge. |
| 48 | +integration = aws.apigatewayv2.Integration("integration", |
| 49 | + api_id=api.id, |
| 50 | + |
| 51 | + # The integration type and subtype. |
| 52 | + integration_type="AWS_PROXY", |
| 53 | + integration_subtype="EventBridge-PutEvents", |
| 54 | + credentials_arn=api_gateway_role.arn, |
| 55 | + |
| 56 | + # The body of the request to be sent to EventBridge. Note the |
| 57 | + # event source matches pattern defined on the EventRule, and the |
| 58 | + # Detail expression, which just forwards the body of the original |
| 59 | + # API Gateway request (i.e., the uploaded document). |
| 60 | + request_parameters={ |
| 61 | + "EventBusName": bus.name.apply(lambda name: name), |
| 62 | + "Source": "my-event-source", |
| 63 | + "DetailType": "my-detail-type", |
| 64 | + "Detail": "$request.body", |
| 65 | + }, |
| 66 | +) |
| 67 | + |
| 68 | +# Finally, define the route. |
| 69 | +route = aws.apigatewayv2.Route("route", |
| 70 | + api_id=api.id, |
| 71 | + route_key="POST /uploads", |
| 72 | + target=integration.id.apply(lambda id: f"integrations/{id}"), |
| 73 | +) |
| 74 | + |
| 75 | +# Define a role and policy allowing Lambda functions to log to CloudWatch. |
| 76 | +lambda_role = aws.iam.Role("lambda-role", |
| 77 | + assume_role_policy=json.dumps({ |
| 78 | + "Version": "2012-10-17", |
| 79 | + "Statement": [ |
| 80 | + { |
| 81 | + "Action": "sts:AssumeRole", |
| 82 | + "Principal": { |
| 83 | + "Service": "lambda.amazonaws.com" |
| 84 | + }, |
| 85 | + "Effect": "Allow" |
| 86 | + } |
| 87 | + ] |
| 88 | + }) |
| 89 | +) |
| 90 | +lambda_role_policy = aws.iam.RolePolicy("lambda-role-policy", |
| 91 | + role=lambda_role.id, |
| 92 | + policy=json.dumps({ |
| 93 | + "Version": "2012-10-17", |
| 94 | + "Statement": [{ |
| 95 | + "Effect": "Allow", |
| 96 | + "Action": [ |
| 97 | + "logs:CreateLogGroup", |
| 98 | + "logs:CreateLogStream", |
| 99 | + "logs:PutLogEvents" |
| 100 | + ], |
| 101 | + "Resource": "arn:aws:logs:*:*:*" |
| 102 | + }] |
| 103 | + }) |
| 104 | +) |
| 105 | + |
| 106 | +# Create a Lambda function handler. |
| 107 | +lambda_function = aws.lambda_.Function("lambda", |
| 108 | + role=lambda_role.arn, |
| 109 | + runtime="python3.7", |
| 110 | + handler="handlers.capture_order", |
| 111 | + code=pulumi.AssetArchive({ |
| 112 | + ".": pulumi.FileArchive('./api') |
| 113 | + }) |
| 114 | +) |
| 115 | + |
| 116 | +# Create an EventBridge target associating the event rule with the function. |
| 117 | +lambda_target = aws.cloudwatch.EventTarget("lambda-target", |
| 118 | + arn=lambda_function.arn, |
| 119 | + rule=rule.name, |
| 120 | + event_bus_name=bus.name, |
| 121 | +) |
| 122 | + |
| 123 | +# Give EventBridge permission to invoke the function. |
| 124 | +lambda_permission = aws.lambda_.Permission("lambda-permission", |
| 125 | + action="lambda:InvokeFunction", |
| 126 | + principal="events.amazonaws.com", |
| 127 | + function=lambda_function.arn, |
| 128 | + source_arn=rule.arn, |
| 129 | +) |
| 130 | + |
| 131 | +# Export the API Gateway URL to give us something to POST to. |
| 132 | +pulumi.export("url", pulumi.Output.concat(api.api_endpoint, "/", stage.name)) |
| 133 | + |
0 commit comments