Last active
October 26, 2021 12:12
-
-
Save mnanchev/4a8ccd9ca09b41455327e87ec0cd0f35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First we create forecast dataset with datafrequency of 1 | |
// We use a timeseries AutoML, the target column is costs | |
const forecastDataset = new AwsCustomResource(this, `forecastDataset`, { | |
onUpdate: { | |
service: 'ForecastService', | |
action: 'createDataset', | |
parameters: { | |
Domain: 'CUSTOM', | |
DatasetName: 'amazonForecastDataset', | |
DataFrequency: 'D', | |
Schema: { | |
Attributes: [ | |
{ | |
AttributeName: 'timestamp', | |
AttributeType: 'timestamp', | |
}, | |
{ | |
AttributeName: 'item_id', | |
AttributeType: 'string', | |
}, | |
{ | |
AttributeName: 'account_id', | |
AttributeType: 'string', | |
}, | |
{ | |
AttributeName: 'target_value', | |
AttributeType: 'float', | |
}, | |
], | |
}, | |
DatasetType: 'TARGET_TIME_SERIES', | |
}, | |
physicalResourceId: { id: `forecastDataset` }, | |
}, | |
onDelete: { | |
service: 'ForecastService', | |
action: 'deleteDataset', | |
parameters: { | |
DatasetArn: `arn:aws:forecast:${this.region}:${this.account}:dataset/amazonForecastDataset`, | |
}, | |
}, | |
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }), | |
}); | |
// We create a datasetgroup from the dataset | |
const forecastDatasetGroup = new AwsCustomResource(this, `forecastDatasetGroup`, { | |
onUpdate: { | |
service: 'ForecastService', | |
action: 'createDatasetGroup', | |
parameters: { | |
DatasetGroupName: 'amazonForecastDatasetGroup', | |
Domain: 'CUSTOM', | |
DatasetArns: [`arn:aws:forecast:us-east-1:${this.account}:dataset/amazonForecastDataset`], | |
}, | |
physicalResourceId: { id: `forecastDatasetGroup` }, | |
}, | |
onDelete: { | |
service: 'ForecastService', | |
action: 'deleteDatasetGroup', | |
parameters: { | |
DatasetGroupArn: `arn:aws:forecast:${this.region}:${this.account}:dataset-group/amazonForecastDatasetGroup` /* required */, | |
}, | |
}, | |
policy: AwsCustomResourcePolicy.fromStatements([ | |
new PolicyStatement({ | |
actions: [ | |
'forecast:CreateDatasetGroup', | |
'forecast:DeleteDatasetGroup', | |
'logs:CreateLogGroup', | |
'logs:CreateLogStream', | |
'logs:PutLogEvents', | |
'databrew:StartJobRun', | |
'iam:PassRole', | |
], | |
resources: ['*'], | |
}), | |
]), | |
}); | |
forecastDatasetGroup.node.addDependency(forecastDataset); | |
// Here we import the csv dataset from S3. This can took up to 40 min | |
const datasetImportJob = new AwsCustomResource(this, `forecastDatasetImportJob`, { | |
onUpdate: { | |
service: 'ForecastService', | |
action: 'createDatasetImportJob', | |
parameters: { | |
DataSource: { | |
S3Config: { | |
Path: `s3://${outputBucket.bucketName}/${ForecastingProperties.PREFIX}-output`, | |
RoleArn: `${dataBrewRole.roleArn}`, | |
}, | |
}, | |
DatasetImportJobName: 'amazonForecastDatasetImportJob', | |
TimestampFormat: 'yyyy-MM-dd', | |
DatasetArn: forecastDataset.getResponseField('DatasetArn'), | |
}, | |
physicalResourceId: { id: `forecastDatasetImportJob` }, | |
}, | |
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }), | |
}); | |
datasetImportJob.node.addDependency(forecastDatasetGroup); | |
// Last we train the model, which can take up to 2 hours | |
new AwsCustomResource(this, `forecastPredictor`, { | |
onUpdate: { | |
service: 'ForecastService', | |
action: 'createPredictor', | |
parameters: { | |
PredictorName: `costAndUsageReportTrainPredictor`, | |
ForecastHorizon: 7, | |
FeaturizationConfig: { | |
ForecastFrequency: 'D', | |
ForecastDimensions: ['account_id'], | |
}, | |
PerformAutoML: true, | |
InputDataConfig: { | |
DatasetGroupArn: `arn:aws:forecast:${this.region}:${this.account}:dataset-group/amazonForecastDatasetGroup`, | |
}, | |
}, | |
physicalResourceId: { id: `forecastPredictor` }, | |
}, | |
onDelete: { | |
service: 'ForecastService', | |
action: 'deletePredictor', | |
parameters: { | |
PredictorArn: `arn:aws:forecast:${this.region}:${this.account}:predictor/costAndUsageReportTrainPredictor`, | |
}, | |
}, | |
policy: AwsCustomResourcePolicy.fromSdkCalls({ resources: AwsCustomResourcePolicy.ANY_RESOURCE }), | |
}).node.addDependency(forecastDatasetGroup); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment