Output monthly AWS cost to Slack channel every day.
These are processed based on AWS Lambda and CloudWatch Events, and managed using AWS CDK. The Slack Incoming WebHook URL must be registered with AWS Systems Manager.
aws configure --profile ${YOUR_AWS_PROFILE_NAME}
https://slack.com/help/articles/115005265063
ex) https://hooks.slack.com/services/KLG439D/KGS341928/Lkfgja2LKGDS2k3j4
Operation with AWS root account is required on AWS Console. https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/ce-access.html
npm install
cdk bootstrap --profile ${YOUR_AWS_PROFILE_NAME}
Register to AWS Systems Manager(SSM)
IAM Policy for cost watcher
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ce:getCostAndUsage",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "iam:ListAccountAliases",
"Resource": "*"
}
]
}
aws ssm put-parameter \
--profile ${YOUR_AWS_PROFILE_NAME} \
--type SecureString \
--name "/CreatedByCDK/AwsCostWatch/Targets/<aws-account-name>/AccessKeyId" \
--value "<your-access-key-id>"
aws ssm put-parameter \
--profile ${YOUR_AWS_PROFILE_NAME} \
--type SecureString \
--name "/CreatedByCDK/AwsCostWatch/Targets/<aws-account-name>/SecretAccessKey" \
--value "<your-secret-access-key>"
It is possible to register multiple accounts with different account names
aws ssm put-parameter \
--profile ${YOUR_AWS_PROFILE_NAME} \
--type SecureString \
--name "/CreatedByCDK/AwsCostWatch/Targets/Account2/AccessKeyId" \
--value "JIO9924lkJSURLKJkfasjdfsaf"
aws ssm put-parameter \
--profile ${YOUR_AWS_PROFILE_NAME} \
--type SecureString \
--name "/CreatedByCDK/AwsCostWatch/Targets/Account2/SecretAccessKey" \
--value "KSLJ434TLKWTJSE45325U0SGKIJLRKJ643TLKEAJIGLSG"
Register to AWS Systems Manager(SSM)
aws ssm put-parameter \
--profile ${YOUR_AWS_PROFILE_NAME} \
--cli-input-json '{"Name": "/CreatedByCDK/AwsCostWatch/SlackWebHookUrl", "Type": "SecureString", "Value": "https://hooks.slack.com/services/KLG439D/KGS341928/Lkfgja2LKGDS2k3j4", "Description": "Incoming Webhook URL for slack.com"}'
npm run build && cdk deploy --profile ${YOUR_AWS_PROFILE_NAME}
Automatically posted on the Slack channel at 10:00 AM JST.
If you don't use it, destroy everything.
cdk destroy --profile ${YOUR_AWS_PROFILE_NAME}
export AWS_PROFILE=<your-configure-name>
FUNCTION_NAME=`aws lambda list-functions | jq -r '.Functions[] | select(.Handler == "lifeguard.handler") | .FunctionName'`
aws lambda invoke --function-name $FUNCTION_NAME /dev/null --log-type Tail --query 'LogResult' --output text | base64 -d