Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our AmazonDynamoDB Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module AmazonDynamoDBCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module AmazonDynamoDBCmdlets;
You can then use the Connect-AmazonDynamoDB cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-AmazonDynamoDB -AWSAccessKey "AccessKey" -AWSSecretKey "MyAccessSecret" -Domain "amazonaws.com" -AWSRegion "OREGON"
Connecting to DynamoDB
Specify the following to connect to data:
- Domain: Set this if you want to use a domain name you have associated with AWS.
- AWSRegion: Set this to the region where your Amazon DynamoDB data is hosted.
Authenticating to DynamoDB
Obtain AWS Keys
To obtain the credentials for an IAM user:- Sign into the IAM console.
- In the navigation pane, select Users.
- To create or manage the access keys for a user, select the user and then go to the Security Credentials tab.
- Sign into the AWS Management console with the credentials for your root account.
- Select your account name or number.
- In the menu that displays, select My Security Credentials.
- To manage or create root account access keys, click Continue to Security Credentials and expand the "Access Keys" section.
Root Credentials
To authenticate using account root credentials, set these configuration parameters:
- AuthScheme: AwsRootKeys.
- AWSAccessKey: The access key associated with the AWS root account.
- AWSSecretKey: The secret key associated with the AWS root account.
Note: Use of this authentication scheme is discouraged by Amazon for anything but simple tests. The account root credentials have the full permissions of the user, making this the least secure authentication method.
If multi-factor authentication is required, specify the following:
- CredentialsLocation: The location of the settings file where MFA credentials are saved. See the Credentials File Location page under Connection String Options for more information.
- MFASerialNumber: The serial number of the MFA device if one is being used.
- MFAToken: The temporary token available from your MFA device.
Note: If you want to control the duration of the temporary credentials, set the TemporaryTokenDuration property (default: 3600 seconds).
Temporary Credentials
To authenticate using temporary credentials, specify the following:
- AuthScheme: TemporaryCredentials.
- AWSAccessKey: The access key of the IAM user to assume the role for.
- AWSSecretKey: The secret key of the IAM user to assume the role for.
- AWSSessionToken: Your AWS session token, provided with your temporary credentials. For details, see AWS Identity and Access Management User Guide.
The cmdlet can now request resources using the same permissions provided by long-term credentials (such as IAM user credentials) for the lifespan of the temporary credentials.
To authenticate using both temporary credentials and an IAM role, set all the parameters described above, and specify these additional parameters:
- AWSRoleARN: Specify the Role ARN for the role you'd like to authenticate with. This prompts the cmdlet to retrieve credentials for the specified role.
- AWSExternalId (optional): Only required if you are assuming a role in another AWS account.
If multi-factor authentication is required, specify the following:
- CredentialsLocation: The location of the settings file where MFA credentials are saved. See the Credentials File Location page under Connection String Options for more information.
- MFASerialNumber: The serial number of the MFA device if one is being used.
- MFAToken: The temporary token available from your MFA device.
Note: If you want to control the duration of the temporary credentials, set the TemporaryTokenDuration property (default: 3600 seconds).
EC2 Instances
Set AuthScheme to AwsEC2Roles.
If you are using the cmdlet from an EC2 Instance and have an IAM Role assigned to the instance, you can use the IAM Role to authenticate. Since the cmdlet automatically obtains your IAM Role credentials and authenticates with them, it is not necessary to specify AWSAccessKey and AWSSecretKey.
If you are also using an IAM role to authenticate, you must additionally specify the following:
- AWSRoleARN: Specify the Role ARN for the role you'd like to authenticate with. This will cause the cmdlet to attempt to retrieve credentials for the specified role.
- AWSExternalId (optional): Only required if you are assuming a role in another AWS account.
IMDSv2 Support
The Amazon DynamoDB cmdlet now supports IMDSv2. Unlike IMDSv1, the new version requires an authentication token. Endpoints and response are the same in both versions.
In IMDSv2, the Amazon DynamoDB cmdlet first attempts to retrieve the IMDSv2 metadata token and then uses it to call AWS metadata endpoints. If it is unable to retrieve the token, the cmdlet reverts to IMDSv1.
AWS Web Identity
Set AuthScheme to AwsWebIdentity.
If you are using the cmdlet from a container configured to assume role with web identity (such as a Pod in an EKS cluster with an OpenID Provider) or have obtained an identity token by authenticating with a web identity provider associated with an IAM role, you can exchange the web identity token and IAM role information for temporary security credentials to authenticate and access AWS services. The cmdlet automatically obtains the credentials if the container has AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE specified in the environment variables. Alternatively, you can specify both AWSRoleARN and AWSWebIdentityToken to execute the AssumeRoleWithWebIdentity API operation and authenticate.
AWS IAM Roles
Set AuthScheme to AwsIAMRoles.
In many situations, it may be preferable to use an IAM role for authentication instead of the direct security credentials of an AWS root user. If you are specifying the AWSAccessKey and AWSSecretKey of an AWS root user, you may not use roles.
To authenticate as an AWS role, set these properties:
- AWSAccessKey: The access key of the IAM user to assume the role for.
- AWSSecretKey: The secret key of the IAM user to assume the role for.
- AWSRoleARN: Specify the Role ARN for the role you'd like to authenticate with. This will cause the cmdlet to attempt to retrieve credentials for the specified role.
- AWSExternalId (optional): Only required if you are assuming a role in another AWS account.
If multi-factor authentication is required, specify the following:
- CredentialsLocation: The location of the settings file where MFA credentials are saved. See the Credentials File Location page under Connection String Options for more information.
- MFASerialNumber: The serial number of the MFA device if one is being used.
- MFAToken: The temporary token available from your MFA device.
Note: If you want to control the duration of the temporary credentials, set the TemporaryTokenDuration property (default: 3600 seconds).
ADFS
To connect to ADFS, set the AuthScheme to ADFS, and set these properties:
- User: The ADFS user.
- Password: The ADFS user's password.
- SSOLoginURL: The SSO provider's login url.
Example connection string:
AuthScheme=ADFS; AWSRegion=Ireland; [email protected]; Password=CH8WerW121235647iCa6; SSOLoginURL='https://adfs.domain.com'; AWSRoleArn=arn:aws:iam::1234:role/ADFS_SSO; AWSPrincipalArn=arn:aws:iam::1234:saml-provider/ADFSProvider; S3StagingDirectory=s3://athena/staging;
ADFS Integrated
The ADFS Integrated flow indicates you are connecting with the currently logged in Windows user credentials. To use the ADFS Integrated flow, do not specify the User and Password, but otherwise follow the same steps in the ADFS guide above.
Okta
To connect to Okta, set the AuthScheme to Okta, and set these properties:
- User: The Okta user.
- Password: The Okta user's password.
- SSOLoginURL: The SSO provider's login URL.
If you are using a trusted application or proxy that overrides the Okta client request OR configuring MFA, you must use combinations of SSOProperties to authenticate using Okta. Set any of the following, as applicable:
- APIToken: When authenticating a user via a trusted application or proxy that overrides the Okta client request context, set this to the API Token the customer created from the Okta organization.
- MFAType: If you have configured the MFA flow, set this to one of the following supported types: OktaVerify, Email, or SMS.
- MFAPassCode: If you have configured the MFA flow, set this to a valid passcode.
If you set this to empty or an invalid value, the cmdlet issues a one-time password challenge to your device or email. After the passcode is received, reopen the connection where the retrieved one-time password value is set to the MFAPassCode connection property. - MFARememberDevice: True by default. Okta supports remembering devices when MFA is required. If remembering devices is allowed according to the configured authentication policies, the cmdlet sends a device token to extend MFA authentication lifetime. If you do not want MFA to be remembered, set this variable to False.
Example connection string:
AuthScheme=Okta; AWSRegion=Ireland; [email protected]; Password=CH8WerW121235647iCa6; SSOLoginURL='https://cdata-us.okta.com/home/amazon_aws/0oa35m8arsAL5f5NrE6NdA356/272'; SSOProperties='ApiToken=01230GGG2ceAnm_tPAf4MhiMELXZ0L0N1pAYrO1VR-hGQSf;'; AWSRoleArn=arn:aws:iam::1234:role/Okta_SSO; AWSPrincipalARN=arn:aws:iam::1234:saml-provider/OktaProvider; S3StagingDirectory=s3://athena/staging;
To connect to PingFederate, set AuthScheme to PingFederate, and set these properties:
- User: The PingFederate user.
- Password: The PingFederate user's password.
- SSOLoginURL: The SSO provider's login url.
- AWSRoleARN (optional): If you have multiple role ARNs, specify the one you want to use for authorization.
- AWSPrincipalARN (optional): If you have multiple principal ARNs, specify the one you want to use for authorization.
- SSOExchangeUrl: The Partner Service Identifier URI configured in your PingFederate server instance under: SP Connections > SP Connection > WS-Trust > Protocol Settings. This should uniquely identify a PingFederate SP Connection, so it is a good idea to set it to your AWS SSO ACS URL. You can find it under AWS SSO > Settings > View Details next to the Authentication field.
- SSOProperties (optional): Authscheme=Basic if you want to include your username and password as an authorization header in requests to Amazon S3.
To enable mutual SSL authentication for SSOLoginURL, the WS-Trust STS endpoint, configure these SSOProperties:
- SSLClientCert
- SSLClientCertType
- SSLClientCertSubject
- SSLClientCertPassword
Example connection string:
authScheme=pingfederate;SSOLoginURL=https://mycustomserver.com:9033/idp/sts.wst;SSOExchangeUrl=https://us-east-1.signin.aws.amazon.com/platform/saml/acs/764ef411-xxxxxx;user=admin;password=PassValue;AWSPrincipalARN=arn:aws:iam::215338515180:saml-provider/pingFederate;AWSRoleArn=arn:aws:iam::215338515180:role/SSOTest2;
Credentials Files
You can use a credentials file to authenticate. Any configurations related to AccessKey/SecretKey authentication, temporary credentials, role authentication, or MFA can be used. To do so, set the following properties to authenticate:
- AuthScheme: AwsCredentialsFile.
- AWSCredentialsFile: The location of your credentials file.
- AWSCredentialsFileProfile (optional): The name of the profile you would like to use from the specified credentials file. If not specified, the default profile is used.
AWS Cognito Credentials
If you want to use the cmdlet with a user registered in a User Pool in AWS Cognito, set the following properties to authenticate:
- AuthScheme: Set this to AwsCognitoSrp (recommended). You can also use AwsCognitoBasic.
- AWSCognitoRegion: Set this to the region of the User Pool.
- AWSUserPoolId: Set this to the User Pool Id.
- AWSUserPoolClientAppId: Set this to the User Pool Client App Id.
- AWSUserPoolClientAppSecret: Set this to the User Pool Client Secret.
- AWSIdentityPoolId: Set this to the Identity Pool Id of the Identity Pool that is linked with the User Pool.
- User: Set this to the username of the user registered in the User Pool.
- Password: Set this to the password of the user registered in the User Pool.
Retrieving Data
The Select-AmazonDynamoDB cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-AmazonDynamoDB -Connection $conn -Table "Account" -Columns @("Id, Name") -Where "FirstName='Bob'"The Invoke-AmazonDynamoDB cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-AmazonDynamoDB -Connection $conn -Table Account -Where "FirstName <> 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountData.csv -NoTypeInformation
You will notice that we piped the results from Select-AmazonDynamoDB into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-AmazonDynamoDB -AWSAccessKey "AccessKey" -AWSSecretKey "MyAccessSecret" -Domain "amazonaws.com" -AWSRegion "OREGON" PS C:\> $row = Select-AmazonDynamoDB -Connection $conn -Table "Account" -Columns (Id, Name) -Where "FirstName <> 'Bob'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Account", "Columns": [ ], "Id": "MyId", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-AmazonDynamoDB -Connection $conn -Table Account -Where "FirstName = 'Bob'" | Remove-AmazonDynamoDB
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Amazon DynamoDB, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyAccountUpdates.csv | %{ $record = Select-AmazonDynamoDB -Connection $conn -Table Account -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-AmazonDynamoDB -Connection $conn -Table Account -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'" }else{ Add-AmazonDynamoDB -Connection $conn -Table Account -Columns @("Id","Name") -Values @($_.Id, $_.Name) } }