

# How the Parameter Store extension in AWS Systems Manager Cloud Extensions works

The Parameter Store extension caches parameter values in memory and checks Parameter Store for changes after the cache time to live (TTL) expires.

1. **First read** – When your application asks for a parameter that the cache doesn't hold, the extension gets the parameter from Parameter Store and stores it in memory.

1. **Cached reads** – The extension serves later reads from the cache until the cache TTL expires.

1. **Checks after the TTL** – After the TTL expires, the extension checks Parameter Store for the parameter. If the parameter changed, the extension serves the new value.

1. **Cache eviction** – When the cache reaches the cache size, the extension drops the entry that you read least recently.

**Topics**
+ [Decryption](#cloud-extensions-ext-parameter-store-how-it-works-decryption)
+ [How GetParameters uses the cache](#cloud-extensions-ext-parameter-store-how-it-works-how-getparameters-uses-the-cache)
+ [How the extension reacts to errors](#cloud-extensions-ext-parameter-store-how-it-works-how-the-extension-reacts-to-errors)

## Decryption
<a name="cloud-extensions-ext-parameter-store-how-it-works-decryption"></a>

The extension reads SecureString parameters with the permissions of the role that it uses. That role is the IAM execution role of the profile entry or the credentials of the runtime. Without `WithDecryption` set to `true`, the extension returns the encrypted value. The extension caches the encrypted value and the decrypted value as separate entries. If the extension can't use the AWS Key Management Service (AWS KMS) key, the extension caches the error for one TTL.

## How GetParameters uses the cache
<a name="cloud-extensions-ext-parameter-store-how-it-works-how-getparameters-uses-the-cache"></a>

GetParameters shares its cache entries with GetParameter, so a value that one operation fetches serves the other.
+ **Every name is fresh** – The extension answers the request from the cache, with no call to Parameter Store.
+ **Any name isn't fresh** – The extension sends every name in the request to Parameter Store in one `GetParameters` call and stores the values that Parameter Store returns. The extension doesn't combine cached values with values from Parameter Store in one response.
+ **A name in `InvalidParameters`** – When Parameter Store lists a name as not valid, the extension drops the cached value of the name and returns the name in `InvalidParameters`. The extension doesn't store an error for the name.
+ **A stored error** – GetParameters never answers from an error that GetParameter stored. The name isn't fresh, so the extension sends the request to Parameter Store.
+ **A transient failure** – The call can fail with `ThrottlingException`, `InternalServerError`, or `ServiceUnavailable`, time out, fail to connect, or fail with `InvalidKeyId` for the whole request. If every name has a cached value, the extension returns the cached values with status `200 OK`, and treats them as fresh for another TTL. If any name has no cached value, the extension returns the error for the whole request, as Parameter Store does. A timeout or a failed connection is then `502 BadGatewayException`. The extension never returns part of the names, and never lists the names of a failed call in `InvalidParameters`. Either way, the extension drops and stores nothing, so a name that was fresh stays fresh.
+ **InvalidKeyId** – GetParameters treats `InvalidKeyId` for the whole request as a transient failure. GetParameter treats it as a definite answer. Parameter Store returns `InvalidKeyId` for a whole `GetParameters` request when it can't use AWS KMS at the time, so the cached values are still good. A SecureString whose KMS key is disabled or deleted is listed in `InvalidParameters` instead, and loses its cached value.
+ **A refused request** – If Parameter Store refuses the whole request with `AccessDeniedException` or `ValidationException`, the extension drops the cached value of every name and returns the error.
+ **A credentials or signing error** – The extension returns the error, doesn't serve any cached value, and leaves the entries as they were.

For example, a request names 10 parameters. 4 of them are fresh in the cache, and 6 aren't cached:

1. The cache doesn't answer the request, because 6 names aren't fresh. The extension sends all 10 names to Parameter Store in one `GetParameters` call.

1. If the call succeeds, the extension returns the answer of Parameter Store, and stores all 10 values, fresh for one TTL.

1. If Parameter Store throttles the call, the extension returns `ThrottlingException` for the whole request, because 6 names have no cached value to serve. It doesn't return the 4 cached values, and it doesn't list the 6 names in `InvalidParameters`. The 4 entries stay fresh, so a GetParameter read of one of them is still served from the cache.

1. If all 10 names have a cached value instead, fresh or stale, the extension returns all 10 cached values with status `200 OK`.

## How the extension reacts to errors
<a name="cloud-extensions-ext-parameter-store-how-it-works-how-the-extension-reacts-to-errors"></a>

These rules apply only when a read calls Parameter Store. A read calls Parameter Store on a cache miss, and on every read when **Cache TTL** is 0. The extension serves a fresh cache entry without calling Parameter Store, whether the entry holds a value or a stored error. For GetParameters, see also [How GetParameters uses the cache](#cloud-extensions-ext-parameter-store-how-it-works-how-getparameters-uses-the-cache).

The extension sorts the results from Parameter Store into three groups and reacts to each group differently:


| Result from Parameter Store | The cache holds a stale value | The cache holds nothing | 
| --- | --- | --- | 
| **Definite answers:** `ParameterNotFound`, `ParameterVersionNotFound`, `AccessDeniedException`, `InvalidKeyId` (transient for a whole GetParameters request), `ValidationException`, `ValidationError` | The extension drops the stale value. The extension returns the error and stores the error for **Cache TTL** seconds. The extension doesn't store the error when the TTL is 0. | The extension returns the error and stores the error for **Cache TTL** seconds. The extension doesn't store the error when the TTL is 0. | 
| **Transient failures:** `ThrottlingException`, `InternalServerError` (including a canceled call, and a call that the runtime fails for a reason other than credentials), `ServiceUnavailable`, `RequestTimeoutException`, `RequestAbortedException`, a timeout, a failed connection, or a response that the extension can't read | The extension serves the stale value and treats the value as fresh for another TTL. | The extension returns the error and stores nothing. A timeout, a failed connection, or a response that the extension can't read is `502 BadGatewayException`. The next read calls Parameter Store again. | 
| **Credentials, signing, and refused requests:** `InternalFailure`, `ExpiredTokenException`, `UnrecognizedClientException`, `InvalidSignatureException`, `IncompleteSignature`, `IncompleteSignatureException`, `MissingAuthenticationTokenException`, `NotAuthorized`, `OptInRequired`, `MalformedHttpRequestException`, `RequestEntityTooLargeException`, `UnknownOperationException` | The extension returns the error. The extension doesn't serve the stale value, and it leaves the entry as it was. The next read calls Parameter Store again. | The extension returns the error and stores nothing. The next read calls Parameter Store again. | 

This behavior has the following results:
+ **Outages don't break your application** – If Parameter Store throttles requests, returns errors, is unavailable, or can't be reached, you keep getting the last good value.
+ **The extension respects revoked access** – When Parameter Store answers `AccessDeniedException`, `InvalidKeyId`, or `ParameterNotFound`, the extension drops the cached value. This happens at the first call to Parameter Store after the value goes stale, within about **Cache TTL** seconds. The extension never serves a stale value when it can't get credentials, or when Parameter Store rejects the credentials.
+ **The extension caches errors too** – The extension caches a not-found result for **Cache TTL** seconds. If you create the parameter right after a miss, the extension keeps answering `ParameterNotFound` until that TTL expires.
+ **The extension can serve a stale value indefinitely** – If Parameter Store keeps failing for transient reasons, the extension keeps serving the stale value. The extension stops when Parameter Store returns a value or a definite answer, or when the entry leaves the cache.
+ **Reads drive the checks** – The extension doesn't refresh entries in the background. After a transient failure, the first read after the extended TTL calls Parameter Store again. Until then, the extension serves reads from the cache. If your application stops reading a parameter, the extension makes no calls for it.
+ **The extension doesn't back off from throttling when the cache holds nothing** – While Parameter Store throttles, every read of that parameter calls Parameter Store again.
+ **Without the capability, the extension bypasses the cache** – If you don't approve the `parameterStoreRead` capability, the extension neither serves from the cache nor stores values in the cache.