Skip to content

Commit

Permalink
#1 working cloud formation doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jimzucker committed Dec 20, 2020
1 parent e9ff6e3 commit 4fdc8a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
6 changes: 5 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
aws s3 --profile jim-zucker cp get_forecast.py s3://jimzucker-github-getforecast
rm -fr target
mkdir target
cd target
zip get_forecast.zip ../get_forecast.py
aws s3 --profile jim-zucker cp get_forecast.zip s3://jimzucker-github-getforecast
14 changes: 6 additions & 8 deletions get_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ def calc_forecast(boto3_session):
}
}

#get accountname for organization
org_account_id = sts.get_caller_identity().get('Account')
org_name=org.describe_account(AccountId=org_account_id)['Account']['Name']


utcnow = datetime.datetime.utcnow()
today = utcnow.strftime('%Y-%m-%d')
first_day_of_month = utcnow.strftime('%Y-%m') + "-01"
Expand Down Expand Up @@ -217,7 +212,7 @@ def calc_forecast(boto3_session):
"account_name": 'Total',
"amount_usage": amount_usage,
"amount_forecast": amount_forecast,
"forecast_variance": (amount_forecast-amount_usage_prior_month) / amount_usage_prior_month *100
"forecast_variance": forecast_variance
}
output=[]
output.append(result)
Expand Down Expand Up @@ -286,7 +281,11 @@ def calc_forecast(boto3_session):
if amount_usage_prior_month > 0 :
variance = (amount_forecast-amount_usage_prior_month) / amount_usage_prior_month *100

account_name=org.describe_account(AccountId=linked_account)['Account']['Name']
try:
account_name=org.describe_account(AccountId=linked_account)['Account']['Name']
except AWSOrganizationsNotInUseException as e:
account_name=linked_account

result = {
"account_name": account_name,
"amount_usage": amount_usage,
Expand Down Expand Up @@ -330,7 +329,6 @@ def format_rows(output,account_width):
return output_rows

def publish_forecast(boto3_session) :

#read params
columns_displayed = ["Account", "MTD", "Forecast", "Change"]
if 'GET_FORECAST_COLUMNS_DISPLAYED' in os.environ:
Expand Down
23 changes: 12 additions & 11 deletions get_forecast_cf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Resources:
Action:
- logs:*
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- organizations:DescribeAccount
Resource: "*"
- Effect: Allow
Action:
- ce:GetCostAndUsage
Resource: "*"
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'

Expand All @@ -63,19 +71,11 @@ Resources:
FunctionName: getForecastLambda
Role: !GetAtt getForecastFunctionRole.Arn
Timeout: 30
Handler: index.handler
Handler: get_forecast.lambda_handler
Runtime: python3.6
Code:
ZipFile: |
import json
import boto3
import traceback
def handler(event, context):
try:
print(json.dumps(event))
return
except Exception as e:
traceback.print_exc()
S3Bucket: jimzucker-github-getforecast
S3Key: get_forecast.zip
Description: Post current forecast to slack.

getForecastLambdaPermission:
Expand All @@ -91,6 +91,7 @@ Resources:
# so we define it to ensure lgos roll
getForecastLambdaLogGroup:
Type: 'AWS::Logs::LogGroup'
DependsOn: getForecastLambda
Properties:
LogGroupName: !Sub "/aws/lambda/${getForecastLambda}"
RetentionInDays: '7'
Expand Down

0 comments on commit 4fdc8a5

Please sign in to comment.