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

chore: update IAM README with compiling code samples #1078

Merged
merged 1 commit into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions packages/@aws-cdk/aws-iam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,16 @@
Define a role and add permissions to it. This will automatically create and
attach an IAM policy to the role:

```ts
const role = new Role(this, 'MyRole', {
assumedBy: new ServicePrincipal('sns.amazonaws.com')
});
role.addPermission(new Permission('*', 'lambda:InvokeFunction'));
```
[attaching permissions to role](test/example.role.lit.ts)

Define a policy and attach it to groups, users and roles. Note that it is possible to attach
the policy either by calling `xxx.attachPolicy(policy)` or `policy.attachToXxx(xxx)`.
the policy either by calling `xxx.attachInlinePolicy(policy)` or `policy.attachToXxx(xxx)`.

```ts
const user = new User(this, 'MyUser', { password: '1234' });
const group = new Group(this, 'MyGroup');

const policy = new Policy(this, 'MyPolicy');
policy.attachToUser(user);
group.attachPolicy(policy);
```
[attaching policies to user and group](test/example.attaching.lit.ts)

Managed policies can be attached using `xxx.attachManagedPolicy(arn)`:

```ts
const group = new Group(this, 'MyGroup');
group.attachManagedPolicy('arn:aws:iam::aws:policy/AdministratorAccess');
```
[attaching managed policies](test/example.managedpolicy.lit.ts)

### Features

Expand Down
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-iam/test/example.attaching.lit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cdk = require('@aws-cdk/cdk');
import { Group, Policy, User } from '../lib';

export class ExampleConstruct extends cdk.Construct {
constructor(parent: cdk.Construct, id: string) {
super(parent, id);

/// !show
const user = new User(this, 'MyUser', { password: '1234' });
const group = new Group(this, 'MyGroup');

const policy = new Policy(this, 'MyPolicy');
policy.attachToUser(user);
group.attachInlinePolicy(policy);
/// !hide
}
}
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-iam/test/example.managedpolicy.lit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import cdk = require('@aws-cdk/cdk');
import { Group } from '../lib';

export class ExampleConstruct extends cdk.Construct {
constructor(parent: cdk.Construct, id: string) {
super(parent, id);

/// !show
const group = new Group(this, 'MyGroup');
group.attachManagedPolicy('arn:aws:iam::aws:policy/AdministratorAccess');
/// !hide
}
}
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-iam/test/example.role.lit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cdk = require('@aws-cdk/cdk');
import { PolicyStatement, Role, ServicePrincipal } from '../lib';

export class ExampleConstruct extends cdk.Construct {
constructor(parent: cdk.Construct, id: string) {
super(parent, id);

/// !show
const role = new Role(this, 'MyRole', {
assumedBy: new ServicePrincipal('sns.amazonaws.com')
});

role.addToPolicy(new PolicyStatement()
.addAllResources()
.addAction('lambda:InvokeFunction'));
/// !hide
}
}