Map Bucket, RAM Policy and Object Access
A bucket is an identity before it is a container. The name is globally unique, three to sixty-three characters of lowercase letters, digits and hyphens, and it cannot be renamed after creation. Because the name is global rather than account-scoped, it tells you nothing about which account owns the bytes.
Placement follows the provider, not a bucket argument. In provider 2.2.0 the bucket resource has no location field; the configured region decides where the bucket is created. Changing the region either fails because the global name is already taken or creates a second bucket under a new name; it never moves the first one.
Three permission systems answer three different questions. A bucket policy is attached to the resource and names principals. A RAM policy is attached to an identity and names actions on resources. An ACL is a canned label on a bucket or object and names no principal at all.
| Mechanism | Attached to | Names a principal | Terraform resource |
|---|---|---|---|
| Bucket policy | the bucket | yes, including other accounts and anonymous | alicloud_oss_bucket_policy |
| RAM policy | a user or role | no, the identity is the subject | alicloud_ram_policy |
| Bucket ACL | the bucket | no, only private or public | alicloud_oss_bucket_acl |
| Object ACL | one object | no, and it overrides the bucket ACL | alicloud_oss_bucket_object |
| Block Public Access | account or bucket | no, it suppresses public grants | alicloud_oss_bucket_public_access_block, plus an account variant |
The evaluation is deny-first, and every applicable policy must allow. An explicit deny wins over any allow, whether it comes from RAM, the bucket policy or a control policy. For a same-account identity the RAM policy is the grant; for a cross-account caller the bucket policy must also allow, and a resource-directory control policy can only narrow the result.
Anonymous requests skip RAM entirely. When no identity is presented, OSS evaluates the bucket policy first, then the object ACL, then the bucket ACL. That is why a strict RAM policy does not make a public-read bucket private, and why a public bucket policy can expose objects that no identity was ever granted.
Block Public Access is the outer switch for public exposure. Enabling it makes existing public ACLs and public bucket-policy grants stop taking effect, and refuses new ones. The console enables it by default on new buckets; in Terraform you declare it explicitly, which is the only way the setting appears in review.
| Bucket ACL | Bucket policy | Block Public Access | Anonymous result |
|---|---|---|---|
| private | no public statement | either | denied |
| public-read | no public statement | off | allowed |
| private | wildcard principal, no condition | off | allowed anonymously, owner permission unchanged |
| private | wildcard principal with a condition | off | allowed, and the statement now covers the owner too |
| any public grant | any public grant | on | denied, and the grant has no effect |
The wildcard principal has an owner exception that surprises reviewers. A statement with a bare wildcard and no condition applies to non-owner callers, which already includes anonymous users. Adding a condition widens the same statement to cover the bucket owner as well, so it is not the difference between private and public.
Do not manage one attribute from two resources. The bucket resource still accepts an ACL and a policy argument, both deprecated since provider 1.220.0 in favour of standalone resources. If both forms manage the same attribute, the provider requires an ignore_changes entry; without it, every apply can fight the last one.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Request arrives at the bucket"] --> B{"Identity presented?"}
B -->|"yes"| C["RAM authenticates the caller"]
C --> D{"RAM policy allows the action?"}
D -->|"no"| X["Denied before OSS sees the object"]
D -->|"yes"| E{"Bucket policy explicit deny?"}
E -->|"yes"| X
E -->|"no match or allow"| OK["Request evaluated as allowed"]
B -->|"no"| F{"Block Public Access enabled?"}
F -->|"yes"| X
F -->|"no"| G{"Bucket policy decision"}
G -->|"deny"| X
G -->|"allow"| OK
G -->|"no match"| H{"Object ACL"}
H -->|"private"| X
H -->|"public read"| OK
H -->|"default, inherit"| I{"Bucket ACL"}
I -->|"private"| X
I -->|"public read"| OK
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A,B,C,D,E,F,G,H,I primary;
class OK ok;
class X danger;
Figure 1 separates the identity path from the anonymous path. RAM is consulted only when a caller presents a credential; an anonymous request goes straight to the resource policy and ACLs. Block Public Access sits on the anonymous branch because it changes what a public grant means, not whether RAM allows an action.
Figure 2 shows the gates as separate owners. The identity team owns the RAM policy, the data team owns the bucket policy, and the platform team owns the public switch. A private ACL is only the public gate, so it cannot compensate for a wildcard statement in the bucket policy.
Choose Encryption, Lifecycle and Versioning from Current Docs
Encryption is a data-at-rest control, not an access control. A private ACL keeps anonymous callers out; encryption decides what the stored bytes look like at rest and who must be able to unwrap the key. Treat them as separate decisions with separate owners.
Server-side encryption has two ownership models. SSE-OSS uses AES256 keys that OSS generates and manages, adds no key service dependency, and is the low-friction default. SSE-KMS uses a KMS customer master key, which adds KMS activation, key permissions and a small per-call cost.
| Mode | Key owner | Extra dependency | Cost signal |
|---|---|---|---|
| SSE-OSS | OSS | none beyond the bucket rule | no separate encryption charge |
| SSE-KMS, default key | KMS, OSS-managed | KMS activated in the region | KMS API calls |
| SSE-KMS, customer CMK | you | key policy and caller permission | KMS API calls and key management |
| No bucket rule | neither | object header decides per object | no encryption guarantee |
KMS changes who must be present at read time. A request to a KMS-encrypted bucket needs permission to use the CMK, and an anonymous request cannot satisfy that check. The service returns a KMS-forbidden error rather than an object, which is a useful signal that encryption is doing its job.
Bucket encryption is a default, not a rewrite. Enabling or changing the bucket rule does not re-encrypt objects that already exist. An object uploaded with its own encryption header keeps that method, so a bucket default is only a promise about future writes.
Lifecycle rules are asynchronous cleanup with a daily clock. OSS loads a new rule within 24 hours and then runs rules daily at 08:00 UTC+8. There is no per-run deletion count, log or notification, so verify by listing the target prefix rather than waiting for a completion event.
Every action needs an age and a target. A rule can expire current objects, transition them to a colder class, abort incomplete multipart uploads, expire noncurrent versions, or transition those versions. At least one action is required, and a prefix or tag filter is what keeps a rule away from data it should not touch.
The cheap class is only cheap after its minimum duration. IA has a 30-day minimum, Archive 60 days, and Cold Archive and Deep Cold Archive 180 days. Deleting or transitioning earlier still bills the remainder, so a rule that races down the classes costs more than it saves.
| Class | Minimum duration | Minimum billed size | Read behaviour |
|---|---|---|---|
| Standard | none | actual size | immediate |
| IA | 30 days | 64 KB per object | immediate, retrieval fee |
| Archive | 60 days | 64 KB per object | restore first, typically minutes |
| Cold Archive | 180 days | 64 KB per object | restore in one to twelve hours |
| Deep Cold Archive | 180 days | 64 KB per object | restore in twelve to forty-eight hours |
Region support is part of the storage choice. Not every storage class or KMS key type is offered in every region, and a bucket cannot change region after creation. Record the region beside the class and the key, then check both again before the first apply.
Versioning is a one-way switch with three states. A bucket starts disabled, can be enabled, and can later be suspended, but it can never return to disabled. Enabled keeps every version; suspended writes a null version and keeps the older ones.
A delete in a versioned bucket is usually a marker. Deleting without a version id adds a delete marker, and the previous bytes remain as a noncurrent version. Expiring the current version through lifecycle does the same thing, which is why a rule that looks like deletion may only be hiding the object.
Retention is the setting that survives your change window. A bucket-level WORM policy protects every object for 1 to 25,550 days, starts in an in-progress state for 24 hours, and becomes irreversible once locked. After the lock, the period can be extended but never shortened, and protected deletes return an immutability error.
WORM and versioning interact in one direction. A retention policy requires versioning to be enabled or disabled, not suspended, and after it is enabled the bucket cannot move from enabled to suspended. Lifecycle may still transition protected objects to a colder class, but it cannot expire them inside the retention window.
One Terraform resource per bucket concern. Versioning, encryption, ACL, policy, public access block and WORM all have standalone resources in 2.2.0, while the bucket resource still exposes several of the same fields. Pick the standalone form and add the documented ignore_changes so a drift correction does not become a second owner.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
A["Write the data contract
lifetime, reads, legal hold"] --> B{"Regulated retention?"}
B -->|"yes"| C["Bucket WORM
1 to 25550 days"]
C --> D{"Legal review and
restore drill passed?"}
D -->|"no"| D1["Do not lock: the period
will be permanent"]
D -->|"yes"| E["Lock the policy
extend only, never shorten"]
D1 --> F
E --> F
A --> N{"Who owns the key?"}
N -->|"platform"| O["SSE-OSS
AES256, OSS-managed"]
N -->|"customer"| P["SSE-KMS
CMK in the same region"]
P --> P1{"KMS activated and
CMK permission granted?"}
P1 -->|"no"| P2["Reads fail with a
KMS forbidden error"]
P1 -->|"yes"| O2["Encrypt new objects
existing objects unchanged"]
O --> O2
B -->|"no"| F{"Need overwrite recovery?"}
F -->|"yes"| G["Versioning Enabled
delete markers, noncurrent versions"]
F -->|"no"| H["Versioning disabled
overwrite is permanent"]
G --> I{"Reads must be immediate?"}
I -->|"no"| J["Lifecycle: Standard to IA
then Archive"]
I -->|"yes"| K["Keep Standard or IA"]
J --> L{"Minimum duration met
before expiry?"}
L -->|"no"| L1["Early deletion charges
for the remaining days"]
L -->|"yes"| M["Expire noncurrent versions
and delete markers"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class A,B,D,F,I,L,N,P1 primary;
class C,E,G,J,K secondary;
class M,O,O2 ok;
class D1,L1,P2 danger;
class H warn;
Figure 3 puts the irreversible choices before the cheap ones. Retention and versioning change what recovery is possible; lifecycle only changes where the bytes sit and when they disappear. Read the failure branches as review questions, because each one is a setting that looks reasonable in a diff and only misbehaves once data exists.
Assign Object Uploads and Content Changes to a Data Owner
The bucket is the container; the objects are the cargo. Terraform is good at building the container: name, region, policy, encryption and lifecycle. It is a poor logistics system for moving cargo, because every object it manages becomes a resource in state.
The object resource keeps content in state. Supplying literal content writes the bytes into the state file, while source reads a file from the machine that runs the apply. Every managed object is also refreshed and compared on later plans, so a large key space becomes a large state file and a long plan.
Use the object resource for a small, deliberate bootstrap. A lifecycle marker, a small configuration file, or a pointer that must exist before the pipeline starts is a reasonable fit. Pin its checksum and keep it in a separate root so a routine data sync does not touch the bucket policy.
Bulk data belongs to a pipeline with its own credentials. The pipeline writes through the OSS API or CLI, records a checksum per object, and owns retries, concurrency and cleanup. Its identity is prefix-scoped: put and read on the release prefix, and no delete on production.
Publish by immutable key, then move a pointer. Write the new artifact under a key that contains its content hash, verify the checksum, then update the small manifest that readers resolve. Overwriting the live key gives readers a torn view and leaves no clean rollback target.
A data-owner record is cheaper than an argument later. Name the dataset, the owning team, the write path, the delete authority, the retention class and the restore procedure. That record is what tells the next engineer whether an object is safe to delete.
| Artifact | Owner | Write path | Terraform role |
|---|---|---|---|
| Bucket policy and lifecycle | platform team | reviewed apply | declares and drifts |
| Release artifact | build pipeline | immutable key plus manifest | none, or a small pointer |
| Nightly export | data pipeline | versioned prefix | none |
| Bootstrap file | platform team | object resource with a pinned source | declares and refreshes |
| Ad-hoc upload | nobody | console or CLI | out of scope |
A media team shows the split in one sentence. Terraform owns the bucket and its lifecycle rule; the transcoder owns objects under the renditions prefix; the CDN reads through a bucket policy; nobody runs an apply to publish a video. The bucket outlives every individual artifact, which is exactly why the two lifecycles should not be managed together.
Cross-account upload is a pair of grants. The uploader's RAM policy allows the action on the bucket prefix, and the bucket policy admits the external principal. Remove either and the put fails before the object exists; a successful put is evidence that both sides agree.
Do not import a data lake into Terraform state. Importing existing objects makes the state a second inventory that drifts from the real one. If an object must be declared, declare the pointer or the manifest, not the population.
Distinguish OSS Resources from the Terraform OSS Backend
Two OSS uses share a service name and nothing else. A bucket resource is managed by the Alicloud provider during plan and apply. The OSS backend is part of Terraform core, configured before providers are initialized, and it stores the state file as an object.
The backend block is not a resource and cannot use variables. It is read during init, so it must contain literal values or receive them through partial configuration. That is why a backend copied from a module often fails before the provider has a chance to authenticate.
State locking is a TableStore contract, not a bucket feature. The backend enables locking only when a TableStore table is configured, and that table must have a string primary key named LockID. Without it, two runners can read the same state and apply different plans.
| Dimension | Bucket resource | OSS backend |
|---|---|---|
| Configured in | a resource block | a backend block |
| Read at | plan and apply | init, before providers |
| Creates | bucket, policy, lifecycle | nothing; the bucket and table must exist |
| Locking | not a bucket feature | TableStore table with a LockID key |
| Encryption | bucket default and object header | encrypt = true uses AES256 |
| Failure if absent | the plan creates it | init fails before the first plan |
Encryption of state is a separate switch. Setting encrypt = true makes the backend write the state object with AES256 server-side encryption. It does not encrypt the plan file, the local cache or the console output; those need their own handling.
The state bucket is production infrastructure. Give it its own bucket, its own policy, versioning for history and a public access block. Never point a lifecycle rule at the state prefix, and never put the state bucket under a locked WORM policy: each apply writes a new state object, and a locked policy either refuses the write or keeps every version beyond your ability to prune it.
The backend authenticates separately from the provider. It reads the Alibaba Cloud environment variables directly and supports an assume-role block, but its session expiration is capped at 3600 seconds. A long apply needs a credential source that can refresh, not a longer session.
Bootstrap the backend before you depend on it. The state bucket and the TableStore table must exist before the first init that uses them. Create them in a small bootstrap root with local state, then move that root to the same backend once it is proven.
Do not infer locking from the provider schema. The bucket resource has no lock field, so its schema says nothing about whether two applies are serialized. Only the backend configuration and the TableStore table answer that question.
Figure 4 contrasts a shared state path with two private copies. The backend does not make the bucket safer; it makes one state object the only history and one lock the only writer. That is a different guarantee from the bucket policy, and it is the one that prevents the second apply from erasing the first.
Rehearse Restore and Non-Empty-Bucket Cleanup
Restore means two different operations in OSS. A version restore copies a noncurrent version back to a readable key; an archive restore creates a temporary replica of a frozen object. Neither is a backup: both remain in the same bucket under the same account, so a bucket deletion removes the original and the recovery copy together.
Archive classes are not readable on demand. Archive restores typically finish in about a minute, Cold Archive in one to twelve hours depending on priority, and Deep Cold Archive in twelve to forty-eight hours. Plan that time into the recovery objective rather than discovering it during an incident.
A temporary replica expires. Archive replicas last one to seven days, Cold and Deep Cold Archive replicas up to 365 days. Cold and Deep Cold Archive replicas also bill at the Standard rate while they exist, so extend the window before it lapses or the object returns to frozen and the next read fails.
Deleting a bucket is a different gate from deleting objects. OSS refuses to delete a non-empty bucket, and empty includes every object version, every delete marker, incomplete multipart parts and attached access points. A versioned bucket therefore needs a version-aware cleanup, not a recursive delete of current keys.
The destroy flag is a convenience with a blast radius. The bucket resource can delete objects so that destroy succeeds, and the provider warns that those objects are not recoverable. It suits a disposable lab bucket and is unacceptable for a shared data bucket; confirm what it does to versions in the lab rather than assuming.
Retention outranks the destroy flag. While objects are inside the retention window, a locked WORM policy blocks deletion for every caller, including the bucket owner. A plan that tries to destroy a protected bucket fails rather than overriding the policy, which is the correct behaviour and should be tested before the policy is locked.
Cleanup is a review, not a command. A deletion review names the bucket, the objects and versions, the replication destination, the backup copy and the owner who approved it. If any of those is shared or unknown, the answer is no.
Rehearse Restore and Non-Empty-Bucket Cleanup
Execution status: design-only. This trace was written against Terraform 1.17.1 and aliyun/alicloud 2.2.0 on 6 November 2026. No Alibaba Cloud account, credential or bucket was used, so every expected result below is documented behaviour rather than a measurement.
Prerequisites. A disposable account or resource group, a RAM role scoped to one lab prefix, a second role with no OSS grant, and a bucket name with a unique suffix. Use a dedicated bucket, never a shared data bucket, and leave WORM unlocked for the whole exercise.
- Baseline the bucket. Apply a private bucket with the public access block on, versioning enabled and SSE-OSS. Plan again and expect no changes; record the bucket name, region, encryption mode and versioning state.
- Prove the allowed path. As the scoped role, put one small object under the lab input prefix, then fetch it and compare its checksum. Expect success on both calls.
- Prove the denied paths. As the scoped role, try to read an object under the private prefix; expect a denial. As the unrelated role, try to read the input object; expect a denial. Make an anonymous request to the same object; expect a denial while the public access block is on.
- Prove a version restore. Overwrite the object, list its versions, and copy the noncurrent version to a restored prefix. Compare the restored checksum with the value recorded at upload.
- Prove the delete marker. Delete the object without a version id, then fetch it and expect a not-found response. Remove the marker by version id, then fetch it again and expect the earlier bytes.
- Run the destroy on the disposable bucket. Expect the apply to stop while the bucket still holds objects, versions or parts; record which one the service names.
- Clean the disposable bucket. Delete all versions and incomplete parts, then destroy. Confirm the bucket no longer appears in the bucket list and that the RAM role remains.
- Inspect WORM without locking. Plan an
alicloud_oss_bucket_wormresource with a one-day period and read the diff. Do not apply a lock; a locked policy cannot be shortened or deleted, so the plan is the evidence for this lab.
| Observation | Passing result | Failing result means |
|---|---|---|
| Writer put | object exists under the lab prefix | the RAM policy or bucket policy is missing a grant |
| Scoped role get | checksum matches | the role lacks the object action |
| Anonymous get | denied while the block is on | the block is off or the bucket policy is public |
| Overwrite and restore | both versions list, restored bytes match | versioning was never enabled |
| Delete without version | not found, previous version remains | the bucket is unversioned |
| Destroy while non-empty | destroy stops on a non-empty bucket | cleanup ran before the review |
| Final bucket list | lab bucket absent | an object, version or part survived |
Expected observation. The allowed path succeeds, all three denied paths fail with a permission error, the overwrite leaves two versions, and the non-empty destroy stops before deleting the bucket. A clean final list is the only acceptable cleanup evidence.
Failure check. If the anonymous read succeeds, check the public access block before editing the bucket policy. If the destroy succeeds while versions remain, confirm whether the provider cleared them or whether versioning was never enabled. If the restore checksum differs, the copy used the current version rather than the recorded one.
Cost dimensions. Versioned objects bill for every retained version, Cold and Deep Cold Archive replicas bill at the Standard rate while they exist, and requests during cleanup are billable. Keep the lab small, delete versions the same day, and check the bill for residual early-deletion charges.
Cleanup. Remove every version and part, destroy the lab bucket, delete the disposable roles and the access key, and confirm no archive replica or lifecycle rule remains. Record the final bucket list as the evidence, not the destroy output alone.
Sources. Access evaluation follows the OSS permissions overview and bucket policy guide, encryption follows server-side encryption, and lifecycle follows lifecycle rules.
More sources. Versioning follows versioning states, retention follows WORM policies, restore follows archive restore, and deletion follows bucket deletion. The state path follows the Terraform OSS backend, and resource fields were checked against the provider 2.2.0 documentation.
Next in the Series
In Alibaba Data Services: RDS, PolarDB and Cache Recovery Contracts, we'll explore Choose supported database infrastructure and keep schemas, credentials and data recovery separately governed.