|
| 1 | +--- |
| 2 | +page_title: Managing content in Fastly Object Storage |
| 3 | +subcategory: "Guides" |
| 4 | +--- |
| 5 | +# Managing content in Fastly Object Storage |
| 6 | + |
| 7 | +Content in Fastly Object Storage (buckets and objects) can be managed |
| 8 | +using a combination of this provider (Fastly) and the Amazon Web |
| 9 | +Services (AWS) provider, since Fastly Object Storage provides an AWS |
| 10 | +S3-compatible API. |
| 11 | + |
| 12 | +## Example Configuration |
| 13 | + |
| 14 | +Two HCL files are required. The first, `main.tf`: |
| 15 | + |
| 16 | +```terraform |
| 17 | +terraform { |
| 18 | + required_providers { |
| 19 | + fastly = { |
| 20 | + source = "fastly/fastly" |
| 21 | + version = "~> 7.1.0" |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | +
|
| 26 | +resource "fastly_object_storage_access_keys" "main" { |
| 27 | + description = "FOS Key" |
| 28 | + permission = "read-write-admin" |
| 29 | +} |
| 30 | +
|
| 31 | +module "fos" { |
| 32 | + source = "./fos" |
| 33 | + access_key_id = fastly_object_storage_access_keys.main.id |
| 34 | + secret_key = fastly_object_storage_access_keys.main.secret_key |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +The second will need to be placed in a directory named `fos` and |
| 39 | +named `fos/main.tf`: |
| 40 | + |
| 41 | +```terraform |
| 42 | +terraform { |
| 43 | + required_providers { |
| 44 | + aws = { |
| 45 | + source = "hashicorp/aws" |
| 46 | + version = "= 6.0" |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +provider "aws" { |
| 52 | + access_key = var.access_key_id |
| 53 | + secret_key = var.secret_key |
| 54 | + region = "us-east" |
| 55 | +
|
| 56 | + s3_use_path_style = true |
| 57 | + skip_credentials_validation = true |
| 58 | + skip_metadata_api_check = true |
| 59 | + skip_region_validation = true |
| 60 | + skip_requesting_account_id = true |
| 61 | +
|
| 62 | + endpoints { |
| 63 | + s3 = "https://us-east.object.fastlystorage.app" |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +resource "aws_s3_bucket" "main" { |
| 68 | + bucket = "my-test-bucket-123" |
| 69 | +
|
| 70 | + # Fastly object storage uses different region names |
| 71 | + lifecycle { |
| 72 | + ignore_changes = [region] |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +Note: This example uses the `us-east` region of Fastly Object Storage. |
| 79 | +To use a different region, ensure that the proper region |
| 80 | +code is included in the `endpoints` block above. |
| 81 | + |
| 82 | +## Getting Started |
| 83 | + |
| 84 | +With the example files in place, you'll need to initialize Terraform |
| 85 | +and obtain Fastly Object Storage credentials. To do this, run the following |
| 86 | +commands: |
| 87 | + |
| 88 | +```bash |
| 89 | +export FASTLY_API_KEY=<your Fastly API key here> |
| 90 | +terraform init |
| 91 | +terraform apply -target=fastly_object_storage_access_keys.main |
| 92 | +``` |
| 93 | + |
| 94 | +Note: Terraform will issue a warning because the `-target` option is |
| 95 | +used. This use of that option is safe. |
| 96 | + |
| 97 | +This step will connect to the Fastly API using the Fastly Terraform |
| 98 | +provider and obtain a set of Fastly Object Storage credentials. Those |
| 99 | +credentials will be stored in the Terraform state files (or other |
| 100 | +state storage) and will not be displayed. |
| 101 | + |
| 102 | +This initial step is necessary because the credentials are required by |
| 103 | +the AWS Terraform provider. If Terraform attempts to apply the |
| 104 | +entire configuration without them, the AWS provider will report an error because the |
| 105 | +credentials are missing. |
| 106 | + |
| 107 | +## Completing the Process |
| 108 | + |
| 109 | +With the credentials obtained, a standard Terraform `apply` step can be |
| 110 | +used to create the remaining infrastructure; in this case, a bucket in |
| 111 | +Fastly Object Storage named `my-test-bucket-123`. Terraform will pass |
| 112 | +the Fastly Object Storage credentials to the AWS Terraform provider so |
| 113 | +that it can use them to authenticate its API interactions with the |
| 114 | +Fastly Object Storage system. |
| 115 | + |
| 116 | +```bash |
| 117 | +terraform apply |
| 118 | +``` |
0 commit comments