1. Trust Relationships for Forecast with IAM Service-Linked Roles

    Python

    Service-linked roles (SLRs) are IAM roles provided by AWS that allow AWS services to interact securely with other services on your behalf. They help by following the least privilege principle, giving permissions for the service to access only the resources needed to perform its duties.

    When you use AWS Forecast, the service may require permissions to access other resources. For example, it might need to access Amazon Simple Storage Service (S3) buckets containing your datasets. Rather than defining these permissions manually and creating standard IAM roles, AWS offers service-linked roles which are pre-defined by the service and include all the necessary permissions the service needs.

    The AWS service linked role for Forecast (AWSServiceRoleForForecast) is automatically created for your AWS account the first time you create a Forecast resource, such as a dataset or predictor, if one doesn't exist already.

    In Pulumi, you can facilitate the creation and association of an SLR with a service like AWS Forecast by using the ServiceLinkedRole resource. Let's say you want to ensure the service-linked role for Forecast exists, you could incorporate the aws.iam.ServiceLinkedRole Pulumi resource into your infrastructure code:

    import pulumi import pulumi_aws as aws # Creating a service-linked role specifically for AWS Forecast forecast_slr = aws.iam.ServiceLinkedRole("forecastSLR", aws_service_name="forecast.amazonaws.com", description="A service-linked role for AWS Forecast") # Output the ARN of the service-linked role pulumi.export('forecast_slr_arn', forecast_slr.arn)

    This program defines a single resource: a service-linked role for AWS Forecast. After importing the necessary modules, it calls aws.iam.ServiceLinkedRole to create a service-linked role for the Forecast service.

    • aws_service_name: specifies the AWS service that will use this role; in this case, "forecast.amazonaws.com".
    • description: a human-readable description of the role's purpose within your infrastructure.

    The pulumi.export function is used to make the ARN of the created role accessible outside of Pulumi, perhaps for auditing or use in another tool or cloudformation template.

    Here we are assuming that you have already configured the AWS provider for Pulumi; if not, you need to set it up using Pulumi configuration or environment variables.

    Please be aware that the creation of service-linked roles usually is a one-time operation. If the role already exists (AWS Forecast service has been used before in this account and region), the code will recognize it, and Pulumi won't attempt to create it again. If a service-linked role is needed for a different AWS service, you would adjust the aws_service_name accordingly.

    Remember, this is a foundational piece of setting up secure, least-privilege communication between AWS services. After establishing the role, you'd typically proceed to define your datasets, predictors, and forecasts within the AWS Forecast service, though those actions are beyond the basics of creating service-linked roles and are not covered here.

    The documentation for the ServiceLinkedRole can be found here.