$npx -y skills add hashicorp/agent-skills --skill provider-resourcesImplement Terraform Provider resources and data sources using the Plugin Framework. Use when developing CRUD operations, schema design, state management, and acceptance testing for provider resources.
| 1 | # Terraform Provider Resources Implementation Guide |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This guide covers developing Terraform Provider resources and data sources using the Terraform Plugin Framework. Resources represent infrastructure objects that Terraform manages through Create, Read, Update, and Delete (CRUD) operations. |
| 6 | |
| 7 | **References:** |
| 8 | - [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework) |
| 9 | - [Resource Development](https://developer.hashicorp.com/terraform/plugin/framework/resources) |
| 10 | - [Data Source Development](https://developer.hashicorp.com/terraform/plugin/framework/data-sources) |
| 11 | |
| 12 | ## File Structure |
| 13 | |
| 14 | Resources follow the standard service package structure: |
| 15 | |
| 16 | ``` |
| 17 | internal/service/<service>/ |
| 18 | ├── <resource_name>.go # Resource implementation |
| 19 | ├── <resource_name>_test.go # Acceptance tests |
| 20 | ├── <resource_name>_data_source.go # Data source (if applicable) |
| 21 | ├── find.go # Finder functions |
| 22 | ├── exports_test.go # Test exports |
| 23 | └── service_package_gen.go # Auto-generated registration |
| 24 | ``` |
| 25 | |
| 26 | Documentation structure: |
| 27 | ``` |
| 28 | website/docs/r/ |
| 29 | └── <service>_<resource_name>.html.markdown # Resource documentation |
| 30 | |
| 31 | website/docs/d/ |
| 32 | └── <service>_<resource_name>.html.markdown # Data source documentation |
| 33 | ``` |
| 34 | |
| 35 | ## Resource Structure |
| 36 | |
| 37 | ### SDKv2 Resource Pattern |
| 38 | |
| 39 | ```go |
| 40 | func ResourceExample() *schema.Resource { |
| 41 | return &schema.Resource{ |
| 42 | CreateWithoutTimeout: resourceExampleCreate, |
| 43 | ReadWithoutTimeout: resourceExampleRead, |
| 44 | UpdateWithoutTimeout: resourceExampleUpdate, |
| 45 | DeleteWithoutTimeout: resourceExampleDelete, |
| 46 | |
| 47 | Importer: &schema.ResourceImporter{ |
| 48 | StateContext: schema.ImportStatePassthroughContext, |
| 49 | }, |
| 50 | |
| 51 | Schema: map[string]*schema.Schema{ |
| 52 | "name": { |
| 53 | Type: schema.TypeString, |
| 54 | Required: true, |
| 55 | ForceNew: true, |
| 56 | ValidateFunc: validation.StringLenBetween(1, 255), |
| 57 | }, |
| 58 | "arn": { |
| 59 | Type: schema.TypeString, |
| 60 | Computed: true, |
| 61 | }, |
| 62 | "tags": tftags.TagsSchema(), |
| 63 | "tags_all": tftags.TagsSchemaComputed(), |
| 64 | }, |
| 65 | |
| 66 | CustomizeDiff: verify.SetTagsDiff, |
| 67 | } |
| 68 | } |
| 69 | ``` |
| 70 | |
| 71 | ### Plugin Framework Resource Pattern |
| 72 | |
| 73 | ```go |
| 74 | type resourceExample struct { |
| 75 | framework.ResourceWithConfigure |
| 76 | } |
| 77 | |
| 78 | func (r *resourceExample) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 79 | resp.TypeName = req.ProviderTypeName + "_example" |
| 80 | } |
| 81 | |
| 82 | func (r *resourceExample) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 83 | resp.Schema = schema.Schema{ |
| 84 | Attributes: map[string]schema.Attribute{ |
| 85 | "id": framework.IDAttribute(), |
| 86 | "name": schema.StringAttribute{ |
| 87 | Required: true, |
| 88 | PlanModifiers: []planmodifier.String{ |
| 89 | stringplanmodifier.RequiresReplace(), |
| 90 | }, |
| 91 | Validators: []validator.String{ |
| 92 | stringvalidator.LengthBetween(1, 255), |
| 93 | }, |
| 94 | }, |
| 95 | "arn": schema.StringAttribute{ |
| 96 | Computed: true, |
| 97 | PlanModifiers: []planmodifier.String{ |
| 98 | stringplanmodifier.UseStateForUnknown(), |
| 99 | }, |
| 100 | }, |
| 101 | }, |
| 102 | } |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ## CRUD Operations |
| 107 | |
| 108 | ### Create Operation |
| 109 | |
| 110 | ```go |
| 111 | func (r *resourceExample) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 112 | var data resourceExampleModel |
| 113 | resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 114 | if resp.Diagnostics.HasError() { |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | conn := r.Meta().ExampleClient(ctx) |
| 119 | |
| 120 | input := &example.CreateExampleInput{ |
| 121 | Name: data.Name.ValueStringPointer(), |
| 122 | } |
| 123 | |
| 124 | output, err := conn.CreateExample(ctx, input) |
| 125 | if err != nil { |
| 126 | resp.Diagnostics.AddError( |
| 127 | "Error creating Example", |
| 128 | fmt.Sprintf("Could not create example %s: %s", data.Name.ValueString(), err), |
| 129 | ) |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | data.ID = types.StringPointerValue(output.Id) |
| 134 | data.ARN = types.StringPointerValue(output.Arn) |
| 135 | |
| 136 | resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | ### Read Operation |
| 141 | |
| 142 | ```go |
| 143 | func (r *resourceExample) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 144 | var data resourceExampleModel |
| 145 | resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 146 | if resp.Diagnostics.HasError() { |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | conn := r.Meta().ExampleClient(ctx) |
| 151 | |
| 152 | output, err := findExampleByID(ctx, conn, data.ID.ValueString() |