Skip to content

Commit db22cf9

Browse files
authored
Merge pull request #358 from segmentio/repo-sync
repo sync
2 parents 4b44d24 + d1257cf commit db22cf9

File tree

3 files changed

+85
-86
lines changed

3 files changed

+85
-86
lines changed

src/_data/sidenav/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ sections:
176176
- path: /connections/functions/usage
177177
title: Functions Usage Limits
178178
- path: /connections/functions/aws-apis
179-
title: Calling AWS APIs
179+
title: Functions for AWS APIs
180180
- section_title: Storage Destinations
181181
slug: connections/storage
182182
section:
@@ -329,7 +329,7 @@ sections:
329329
- path: /privacy/user-deletion-and-suppression
330330
title: User Deletion and Suppression
331331
- path: /privacy/account-deletion
332-
title: Account & Data Deletion
332+
title: Account & Data Deletion
333333
- path: /privacy/faq
334334
title: Privacy FAQs
335335
- section_title: Protocols

src/_includes/content/functions/runtime.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The following dependencies are installed in the function environment by default.
3131

3232
Only the [`crypto` Node.js module](https://nodejs.org/dist/latest-v10.x/docs/api/crypto.html ) is included (exposed as `crypto`). [Other built-in Node.js modules](https://nodejs.org/api/modules.html) are not available.
3333

34-
For more information on using the `aws-sdk` module, see [Calling AWS APIs](/docs/connections/functions/aws-apis/).
34+
For more information on using the `aws-sdk` module, see how to [set up functions for calling AWS APIs](/docs/connections/functions/aws-apis/).
3535

3636
### Caching
3737

src/connections/functions/aws-apis.md

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,89 @@
11
---
2-
title: 'Calling AWS APIs'
2+
title: 'Set up functions for calling AWS APIs'
33
integration_type: feature
44
---
55

6-
The [`aws-sdk`](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/welcome.html) module is built-in, which allows you to make calls to AWS services in your own AWS accounts. However, it requires additional setup to ensure that access to your AWS resources is secure. This page describes the process for allowing your functions to securely call AWS APIs in your AWS account.
7-
8-
## Create IAM role in your AWS account
9-
10-
First, you'll need to create an IAM role in your AWS account that your function will assume before making AWS API calls. You need two values ahead of time:
11-
12-
* Principal account ID: This is the ID number for the AWS account that your function runs in. For destination functions, this is `458175278816`, and for source functions, this is `300240842537`.
13-
14-
* External ID: Your IAM role uses this value to restrict who can assume it, in this case, your function. We recommend choosing a long string of at least 32 random characters and treating it as if it were an API key or a password.
15-
16-
Then, create an IAM role in your AWS account with the [minimum set of necessary permissions](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). Add a trust relationship to your role with the following policy, filling in the principal account ID and external ID from above:
17-
18-
```json
19-
{
20-
"Version": "2012-10-17",
21-
"Statement": [
22-
{
23-
"Effect": "Allow",
24-
"Principal": {
25-
"AWS": "<PRINCIPAL_ACCOUNT_ID>"
26-
},
27-
"Action": "sts:AssumeRole",
28-
"Condition": {
29-
"StringEquals": {
30-
"sts:ExternalId": "<EXTERNAL_ID>"
31-
}
6+
The [`aws-sdk`](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/welcome.html){:target="_blank"} module is built-in, which allows you to make calls to AWS services in your own AWS accounts. The AWS SDK requires additional setup to ensure access to your AWS resources is secure. This page describes the process for allowing your functions to securely call AWS APIs in your AWS account.
7+
8+
To set up your functions to call AWS APIs:
9+
1. Create an IAM role in your AWS account that your function will assume before making AWS API calls.
10+
1. Make sure you have these two values:
11+
* **Principal account ID**: This is the ID number for the AWS account that your function runs in. For destination functions, this is `458175278816` and for source functions this is `300240842537`.
12+
* **External ID**: This is the value your IAM role uses to ensure that only your functions have the ability to assume the role. Segment recommends you to choose a long string of at least 32 random characters and treat it as if it were an API key or a password.
13+
2. Create an IAM role in your AWS account with the [minimum set of necessary permissions](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege){:target="_blank"}.
14+
3. Add a trust relationship to your role with the following policy, filling in the principal account ID and external ID from step 1.1:
15+
```json
16+
{
17+
"Version": "2012-10-17",
18+
"Statement": [
19+
{
20+
"Effect": "Allow",
21+
"Principal": {
22+
"AWS": "<PRINCIPAL_ACCOUNT_ID>"
23+
},
24+
"Action": "sts:AssumeRole",
25+
"Condition": {
26+
"StringEquals": {
27+
"sts:ExternalId": "<EXTERNAL_ID>"
28+
}
29+
}
30+
}
31+
]
3232
}
33-
}
34-
]
35-
}
36-
```
37-
38-
## Create your function
39-
40-
Now that you have an IAM role in your AWS account, you can create your source or destination function. We recommend using function settings to make the IAM role configurable. This will allow you use different roles for different instances of your function and to securely store your external ID value by making it a "sensitive" setting:
41-
42-
* "IAM Role ARN": A required string setting that is the ARN for the IAM role above (e.g. "arn:aws:iam::1234567890:role/my-secure-role").
43-
44-
* "IAM Role External ID": A required, sensitive string setting that is the external ID for your IAM role.
45-
46-
Here is an example destination function that uploads each event received to an S3 bucket (configured using an additional "S3 Bucket" setting). It uses the built-in local cache to retain S3 clients between requests to minimize processing time and to allow different instances of the function to use different IAM roles:
47-
48-
```javascript
49-
async function getS3(settings) {
50-
const ttl = 30 * 60 * 1000; // 30 minutes
51-
const key = settings.iamRoleArn + settings.iamRoleExternalId;
52-
53-
return cache.load(key, ttl, async () => {
54-
const sts = new AWS.STS();
55-
56-
const creds = await sts
57-
.assumeRole({
58-
RoleArn: settings.iamRoleArn,
59-
ExternalId: settings.iamRoleExternalId,
60-
RoleSessionName: 'segment-function'
61-
})
62-
.promise()
63-
.then(data => {
64-
return {
65-
accessKeyId: data.Credentials.AccessKeyId,
66-
secretAccessKey: data.Credentials.SecretAccessKey,
67-
sessionToken: data.Credentials.SessionToken
68-
};
33+
```
34+
35+
2. Create your function.
36+
<br> Now that you have an IAM role in your AWS account, you can create your source or destination function. Segment recommends you to use function settings to make the IAM role configurable. This allows you to use different roles for different instances of your function and to securely store your external ID value by making it a "sensitive" setting. Here are the required settings:
37+
* **IAM Role ARN**: A string setting that is the ARN for the IAM role above. For example, `arn:aws:iam::1234567890:role/my-secure-role`.
38+
* **IAM Role External ID**: A sensitive string setting that is the external ID for your IAM role.
39+
40+
Below is an example destination function that uploads each event received to an S3 bucket (configured using an additional "S3 Bucket" setting). It uses the built-in local cache to retain S3 clients between requests to minimize processing time and to allow different instances of the function to use different IAM roles.
41+
42+
```javascript
43+
async function getS3(settings) {
44+
const ttl = 30 * 60 * 1000; // 30 minutes
45+
const key = settings.iamRoleArn + settings.iamRoleExternalId;
46+
47+
return cache.load(key, ttl, async () => {
48+
const sts = new AWS.STS();
49+
50+
const creds = await sts
51+
.assumeRole({
52+
RoleArn: settings.iamRoleArn,
53+
ExternalId: settings.iamRoleExternalId,
54+
RoleSessionName: 'segment-function'
55+
})
56+
.promise()
57+
.then(data => {
58+
return {
59+
accessKeyId: data.Credentials.AccessKeyId,
60+
secretAccessKey: data.Credentials.SecretAccessKey,
61+
sessionToken: data.Credentials.SessionToken
62+
};
63+
})
64+
.catch(err => {
65+
throw err;
66+
});
67+
68+
return new AWS.S3(creds);
6969
});
70+
}
7071

71-
return new AWS.S3(creds);
72-
});
73-
}
74-
75-
async function onTrack(event, settings) {
76-
const s3 = await getS3(settings);
77-
78-
return s3
79-
.putObject({
80-
Bucket: settings.s3Bucket,
81-
Key: `${event.type}/${Date.now()}.json`,
82-
Body: JSON.stringify(event)
83-
})
84-
.promise()
85-
.then(data => {
86-
console.log(data);
87-
});
88-
}
89-
```
90-
72+
async function onTrack(event, settings) {
73+
const s3 = await getS3(settings);
74+
75+
return s3
76+
.putObject({
77+
Bucket: settings.s3Bucket,
78+
Key: `${event.type}/${Date.now()}.json`,
79+
Body: JSON.stringify(event)
80+
})
81+
.promise()
82+
.then(data => {
83+
console.log(data);
84+
})
85+
.catch(err => {
86+
throw err;
87+
});
88+
}
89+
```

0 commit comments

Comments
 (0)