|
| 1 | +Resources: |
| 2 | + EventBus: |
| 3 | + Type: AWS::Events::EventBus |
| 4 | + Properties: |
| 5 | + Name: !Sub "${AWS::StackName}-EventBus" |
| 6 | + |
| 7 | + EventBridgeRole: |
| 8 | + Type: AWS::IAM::Role |
| 9 | + Properties: |
| 10 | + AssumeRolePolicyDocument: |
| 11 | + Version: '2012-10-17' |
| 12 | + Statement: |
| 13 | + - Effect: Allow |
| 14 | + Action: |
| 15 | + - sts:AssumeRole |
| 16 | + Principal: |
| 17 | + Service: |
| 18 | + - appsync.amazonaws.com |
| 19 | + - lambda.amazonaws.com |
| 20 | + |
| 21 | + AppSyncApi: |
| 22 | + Type: AWS::AppSync::GraphQLApi |
| 23 | + Properties: |
| 24 | + Name: AppSyncApi |
| 25 | + AuthenticationType: API_KEY |
| 26 | + |
| 27 | + ApiSchema: |
| 28 | + Type: AWS::AppSync::GraphQLSchema |
| 29 | + Properties: |
| 30 | + ApiId: !GetAtt AppSyncApi.ApiId |
| 31 | + Definition: | |
| 32 | + type EntryDetails { |
| 33 | + ErrorCode: String |
| 34 | + ErrorMessage: String |
| 35 | + EventId: String! |
| 36 | + } |
| 37 | +
|
| 38 | + type PutEventsResult { |
| 39 | + Entries: [EntryDetails!]! |
| 40 | + FailedEntry: Int |
| 41 | + } |
| 42 | +
|
| 43 | + type Query { |
| 44 | + sayHello: PutEventsResult! |
| 45 | + } |
| 46 | +
|
| 47 | + schema { |
| 48 | + query: Query |
| 49 | + } |
| 50 | +
|
| 51 | + AppSyncEventBusDataSource: |
| 52 | + Type: AWS::AppSync::DataSource |
| 53 | + Properties: |
| 54 | + ApiId: !GetAtt AppSyncApi.ApiId |
| 55 | + Name: AppSyncEventBusDataSource |
| 56 | + Type: AMAZON_EVENTBRIDGE |
| 57 | + ServiceRoleArn: !GetAtt EventBridgeRole.Arn |
| 58 | + EventBridgeConfig: |
| 59 | + EventBusArn: !GetAtt 'EventBus.Arn' |
| 60 | + |
| 61 | + AppSyncSayHelloResolver: |
| 62 | + DependsOn: ApiSchema |
| 63 | + Type: AWS::AppSync::Resolver |
| 64 | + Properties: |
| 65 | + ApiId: !GetAtt AppSyncApi.ApiId |
| 66 | + TypeName: Query |
| 67 | + FieldName: sayHello |
| 68 | + DataSourceName: !GetAtt AppSyncEventBusDataSource.Name |
| 69 | + Runtime: |
| 70 | + Name: APPSYNC_JS |
| 71 | + RuntimeVersion: 1.0.0 |
| 72 | + Code: | |
| 73 | + import { util } from '@aws-appsync/utils'; |
| 74 | + export function request(ctx) { |
| 75 | + return { |
| 76 | + "operation" : "PutEvents", |
| 77 | + "events" : [{ |
| 78 | + "source": "com.mycompany.myapp", |
| 79 | + "detail": { |
| 80 | + "key1" : "value1", |
| 81 | + "key2" : "value2" |
| 82 | + }, |
| 83 | + "resources": ["Resource1", "Resource2"], |
| 84 | + "detailType": "myDetailType" |
| 85 | + }] |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | + export function response(ctx) { |
| 90 | + if(ctx.error) |
| 91 | + util.error(ctx.error.message, ctx.error.type, ctx.result) |
| 92 | + else |
| 93 | + return ctx.result |
| 94 | + } |
| 95 | +
|
| 96 | + Connector: |
| 97 | + Type: AWS::Serverless::Connector |
| 98 | + Properties: |
| 99 | + Source: |
| 100 | + Id: AppSyncEventBusDataSource |
| 101 | + Destination: |
| 102 | + Id: EventBus |
| 103 | + Permissions: |
| 104 | + - Write |
| 105 | + |
| 106 | + ApiKey: |
| 107 | + Type: AWS::AppSync::ApiKey |
| 108 | + Properties: |
| 109 | + ApiId: !GetAtt AppSyncApi.ApiId |
| 110 | + |
| 111 | + TriggerFunction: |
| 112 | + Type: AWS::Serverless::Function |
| 113 | + Properties: |
| 114 | + Role: !GetAtt EventBridgeRole.Arn |
| 115 | + Environment: |
| 116 | + Variables: |
| 117 | + API_KEY: !GetAtt ApiKey.ApiKey |
| 118 | + GRAPHQL_URL: !GetAtt AppSyncApi.GraphQLUrl |
| 119 | + EventBusName: !Ref EventBus |
| 120 | + Runtime: nodejs16.x |
| 121 | + Handler: index.handler |
| 122 | + InlineCode: | |
| 123 | + const https = require("https"); |
| 124 | +
|
| 125 | + exports.handler = async () => { |
| 126 | + const queries = { |
| 127 | + sayHello: /* GraphQL */ ` |
| 128 | + query { |
| 129 | + sayHello { |
| 130 | + Entries { |
| 131 | + ErrorCode |
| 132 | + EventId |
| 133 | + ErrorMessage |
| 134 | + } |
| 135 | + FailedEntry |
| 136 | + } |
| 137 | + } |
| 138 | + `, |
| 139 | + }; |
| 140 | +
|
| 141 | + const fetch = async (url, options) => |
| 142 | + new Promise((resolve, reject) => { |
| 143 | + const req = https.request(url, options, (res) => { |
| 144 | + const body = []; |
| 145 | + res.on("data", (chunk) => body.push(chunk)); |
| 146 | + res.on("end", () => { |
| 147 | + const resString = Buffer.concat(body).toString(); |
| 148 | + resolve(resString); |
| 149 | + }); |
| 150 | + }); |
| 151 | + req.on("error", (err) => { |
| 152 | + reject(err); |
| 153 | + }); |
| 154 | + req.on("timeout", () => { |
| 155 | + req.destroy(); |
| 156 | + reject(new Error("Request time out")); |
| 157 | + }); |
| 158 | + req.write(options.body); |
| 159 | + req.end(); |
| 160 | + }); |
| 161 | +
|
| 162 | + const makeRequest = async (queryName) => { |
| 163 | + const options = { |
| 164 | + method: "POST", |
| 165 | + headers: { |
| 166 | + "x-api-key": process.env.API_KEY, |
| 167 | + }, |
| 168 | + body: JSON.stringify({ query: queries[queryName] }), |
| 169 | + timeout: 600000, // ms |
| 170 | + }; |
| 171 | +
|
| 172 | + const response = await fetch(process.env.GRAPHQL_URL, options); |
| 173 | + let body = JSON.parse(response); |
| 174 | + const data = body.data?.[queryName]; |
| 175 | + |
| 176 | + if (body.errors !== undefined) { |
| 177 | + throw JSON.stringify(body.errors); |
| 178 | + } |
| 179 | + |
| 180 | + if (data.FailedEntry != null || data.ErrorCode != null ) { |
| 181 | + throw new Error( |
| 182 | + `${queryName} error: failed to send event to eventbus ${process.env.EventBusName}`); |
| 183 | + } |
| 184 | + |
| 185 | + return body.data; |
| 186 | + }; |
| 187 | +
|
| 188 | + await makeRequest("sayHello"); |
| 189 | + }; |
| 190 | +
|
| 191 | +Metadata: |
| 192 | + SamTransformTest: true |
0 commit comments