STS middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda
Fetches STS credentials to be used when connecting to other AWS services.
To install this middleware you can use NPM:
npm install --save @middy/sts
AwsClient
(object) (defaultAWS.STS
): AWS.STS class constructor (e.g. that has been instrumented with AWS XRay). Must be fromaws-sdk
v2.awsClientOptions
(object) (optional): Options to pass to AWS.STS class constructor.awsClientCapture
(function) (optional): Enable XRay by passingcaptureAWSClient
fromaws-xray-sdk
in.fetchData
(object) (required): Mapping of internal key name to API request parameters.disablePrefetch
(boolean) (defaultfalse
): On cold start requests will trigger early if they can. SettingawsClientAssumeRole
disables prefetch.cacheKey
(string) (defaultsts
): Cache key for the fetched data responses. Must be unique across all middleware.cacheExpiry
(number) (default-1
): How long fetch data responses should be cached for.-1
: cache forever,0
: never cache,n
: cache for n ms.setToContext
(boolean) (defaultfalse
): Store credentials torequest.context
.
NOTES:
- Lambda is required to have IAM permission for
sts:AssumeRole
setToContext
are included for legacy support and should be avoided for performance and security reasons. See main documentation for best practices.
import middy from '@middy/core'
import sts from '@middy/sts'
const handler = middy((event, context) => {
const response = {
statusCode: 200,
headers: {},
body: JSON.stringify({ message: 'hello world' })
};
return response
})
handler
.use(sts({
fetchData: {
assumeRole: {
RoleArn: '...',
RoleSessionName:'' // optional
}
}
}))
For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
Licensed under MIT License. Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the Middy team.