Notify AWS cost to Slack using AWS Lambda + Cost Explorer
You must create an IAM user for deployment.
- AWSLambda_FullAccess
- AmazonS3FullAccess
- CloudWatchEventsFullAccess
- AWSCloudFormationFullAccess
- IAMFullAccess
TODO: Because these policies are too much, it is better to reduce allowed actions.
You can create a Slack app and Incoming Webhook as below:
https://api.slack.com/messaging/webhooks
Webhook URL is required in deployment.
Before building and deployment, samconfig.toml must be set as below:
version = 0.1
[default.global.parameters]
capabilities = "CAPABILITY_IAM"
parameter_overrides = [
"SlackWebhookURL={Slack Webhook URL}",
"ReportingTimezone={Timezone for reporting date e.g. Asia/Tokyo}"
]
profile = "{IAM user profile name for deployment}"
region = "{region}"
s3_bucket = "{S3 bucket name to save Cloud Formation stack}"
stack_name = "cost-notifier-stack"
Compile the code of the Lambda function and build AWS Serverless Application Model (SAM) template.
make build
To avoid complicated local environment settings, Docker is used for compling the code (see here). When you compile the code for the first time, you have to build docker container image at first:
make build-image
Deploy the Lambda function by SAM.
make deploy