Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples with API Gateway #602

Closed
hassankhan opened this issue Aug 18, 2018 · 12 comments · Fixed by #665
Closed

Examples with API Gateway #602

hassankhan opened this issue Aug 18, 2018 · 12 comments · Fixed by #665
Assignees

Comments

@hassankhan
Copy link

Would be awesome if there was some documentation regarding setting up an API Gateway with Lambda-Proxy integration with one or more Lambda functions.

@hassankhan
Copy link
Author

hassankhan commented Aug 19, 2018

On a related note, I've written up an ApiGateway construct that simplifies API creation massively but I'm not quite sure how to add it to the repo or how to write proper tests. Would really appreciate some feedback on whether its worth doubling down on this approach.

What I've got currently allows setting up the following API Gateway resources:

  • AWS::ApiGateway::Account (for enabling CloudWatch Logs for the entire account, also creates an IAM role)
  • AWS::ApiGateway::Deployment (for successive deployments of an API)
  • AWS::ApiGateway::Method (created for each Lambda)
  • AWS::ApiGateway::Resource (created for each Lambda, unless the path is /)
  • AWS::ApiGateway::RestApi (base resource for an API Gateway)
  • AWS::ApiGateway::Stage (only created if caching, logging or metrics is enabled)
export type MethodType = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';

export interface HttpConfig {
  path: string;
  method: MethodType;
  cors?: boolean;
}

export interface LambdaConfig {
  lambdaFn: Lambda.Function
  http: HttpConfig
}

export type EndpointType = 'EDGE' | 'REGIONAL' | 'PRIVATE';

export interface ApiGatewayProps {
  name: string;
  endpoints: LambdaConfig[];
  endpointTypes: EndpointType[];
  cacheEncrypted?: boolean;
  cacheSize?: '0.5' | '1.6' | '6.1' | '13.5' | '28.4' | '58.2' | '118' | '237';
  cacheTtl?: number;
  caching?: boolean;
  logging?: boolean;
  logLevel?: 'OFF' | 'ERROR' | 'INFO';
  metrics?: boolean;
  stageName: string;
}

new ApiGateway(this, 'MyAPI', {
  name: 'MyAPI',
  stageName: 'dev',
  endpointTypes: [ 'EDGE' ],
  logging: true,
  endpoints: [
    {
      lambdaFn: lambdaFn,
      http: {
        path: '/',
        method: 'GET'
      }
    }
  ],
});

@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 20, 2018

@hassankhan, that is amazing! If you really want to contribute this to the CDK standard library, we would gladly accept it! In that case, I would have some style pointers on your code to bring it in line with the rest of our library, and I can tell you about what we expect from testing.

However, before you put in all the effort I believe that @mindstorms6 also had some API-gateway related code lying around, and it would be a waste if you both spent effort on it. So I'll let him chime in first to see where his code is at, and then we can discuss specifics further.

@mindstorms6
Copy link
Contributor

I'm all for this - my efforts have been focused on using the AWS::Serverless::API (for their 'new deployment each time' feature). I think there's a happy spot for both. We can probalby hide them behind the same interface.

Also, looping in @RomainMuller

@hassankhan
Copy link
Author

Awesome, I'll try and get a PR once #474 is resolved 👍

@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 21, 2018

Okay, some comments then:

  • We expect docstrings on all public types, functions and properties (and preferably, also on private ones).
  • TypeScript union types (GET | POST | PUT | ...) don't translate well to other programming languages, and we're targeting all common languages with the API. Use an enum instead.
  • Anything that could conceivably be optional should be (noticing you're already doing this, that's great!)
  • Try not to nest parameters too much. For example, seems like LambdaConfig and HttpConfig could be merged?
  • The cacheSize parameter makes me quite unhappy (numbers in a string) but I suppose that's just the way the API is. Can we find symbolic names for these?

@hpfs74
Copy link

hpfs74 commented Aug 29, 2018

Is the aws-apigateway usable in production? It is really hard to understand how to create a stack that includes a apigateway. A small example would be nice as starting point.

@hassankhan
Copy link
Author

@hpfs74 I'm sure the aws-apigateway construct is production-ready on its own (since it's only wrapping CloudFormation). That said, I'll try and find some time this week to make a PR with the constructs I created, sorry for the delay 👍

@hpfs74
Copy link

hpfs74 commented Aug 31, 2018

That would be awesome @hassankhan

@rhboyd
Copy link
Contributor

rhboyd commented Sep 2, 2018

@mindstorms6 your work has been creating the cfn template with the serverless:transform, and then you let cfn do the actual transform to create the template?

@eladb
Copy link
Contributor

eladb commented Sep 3, 2018

I am working on an API gateway construct library. Stay tuned!

@eladb eladb self-assigned this Sep 3, 2018
@hassankhan
Copy link
Author

Really sorry @eladb, couldn't find the time to make the PR. Is it still worth making one or is it better to leave it to you?

@eladb
Copy link
Contributor

eladb commented Sep 3, 2018

I am on it, would love your feedback when I submit a PR.

eladb pushed a commit that referenced this issue Sep 17, 2018
Introduce an initial construct library for API Gateway. By all means it does not cover 
the entire surface area of API Gateway, but provides basic support for defining 
API gateway configurations (resources/methods hierarchy).

Integration support includes `LambdaIntegration` which accepts a `lambda.Function`, 
sets up the appropriate permissions and binds it to an API method.

Includes many "smart defaults" at every level such as:

- Automatically define a `Deployment` and a `Stage` for the API, which will be recreated every time the API configuration changes (by generating the logical ID of the AWS::ApiGateway::Deployment resource from a hash of the API configuration, similarly to SAM).
- Automatically configure a role for API Gateway to allow writing CloudWatch logs and alarms.
- Specify `defaultIntegration` at the API level which will apply to all methods without integration.

Supports defining APIs like this:

```ts
const integ = new apigw.LambdaIntegration(toysLambdaHandler);
const api = new apigw.RestApi(this, 'my-awesome-api');

api.root.onMethod('GET', new apigw.HttpIntegration('https://amazon.com/toys'));

const toys = api.root.addResource('toys', { defaultIntegration: integ });
toys.addMethod('ANY');

const toy = toys.addResource('{toy}');
toy.addMethod('GET'); // get a toy
toy.addMethod('DELETE'); // remove a toy
```

See [README] for more details.

[README]: https://github.com/awslabs/aws-cdk/blob/c99e7285e7b24700cfd4d52f6da32cffe12c511c/packages/%40aws-cdk/aws-apigateway/README.md

### Framework Changes

 * __resolve__: allow object keys to include tokens, as long as they resolvable to strings.
 * __assert__: invoke `stack.validateTree()` by default for `expect(stack)` to simulate synthesis.

----

* Features not supported yet are listed here: #727, #723
* Fixes #602, copy: @hassankhan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants