Reputation: 77
I am trying to retrieve information with AWS SDK using TypeScript. In Python it was very easy, but I can't figure out how to do this with TypeScript. As you can see, I get the correct information and output it to the console using console.log(data.Outpost?.OutpostArn)
.
But how can I return the data.Outpost?.OutpostArn
from this class? I only get back a response
from which I cannot extract the data.
import * as AWS from 'aws-sdk';
class Outpost {
outpostId: string;
region: string;
constructor(outpostId: string, region: string) {
(this.outpostId = outpostId), (this.region = region);
AWS.config.update({ region: this.region });
}
GetOutpostArn() {
const OutpostArnData = new AWS.Outposts().getOutpost(
{ OutpostId: this.outpostId },
function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data.Outpost?.OutpostArn); // successful response
}
);
debugger;
return OutpostArnData;
}
}
const outpostData = new Outpost(
'op-0e32dade1930682b8',
'us-west-2'
).GetOutpostArn();
console.log(outpostData);
Output:
ubuntu@ip-172-31-20-152:~/FirstTypeScriptCDK$ ts-node lib/outpost.ts
arn:aws:outposts:us-west-2:280066404755:outpost/op-0e32dade1930682b8
and this is the response:
<ref *1> Request {
domain: null,
service: Service {
config: Config {
credentials: [SharedIniFileCredentials],
credentialProvider: [CredentialProviderChain],
region: 'us-west-2',
logger: null,
apiVersions: {},
apiVersion: null,
endpoint: 'outposts.us-west-2.amazonaws.com',
httpOptions: [Object],
maxRetries: undefined,
maxRedirects: 10,
paramValidation: true,
sslEnabled: true,
s3ForcePathStyle: false,
s3BucketEndpoint: false,
s3DisableBodySigning: true,
s3UsEast1RegionalEndpoint: 'legacy',
s3UseArnRegion: undefined,
computeChecksums: true,
convertResponseTypes: true,
correctClockSkew: false,
customUserAgent: null,
dynamoDbCrc32: true,
systemClockOffset: 0,
signatureVersion: 'v4',
signatureCache: true,
retryDelayOptions: {},
useAccelerateEndpoint: false,
clientSideMonitoring: false,
endpointDiscoveryEnabled: undefined,
endpointCacheSize: 1000,
hostPrefixEnabled: true,
stsRegionalEndpoints: 'legacy',
useFipsEndpoint: false,
useDualstackEndpoint: false
},
isGlobalEndpoint: false,
endpoint: Endpoint {
protocol: 'https:',
host: 'outposts.us-west-2.amazonaws.com',
port: 443,
hostname: 'outposts.us-west-2.amazonaws.com',
pathname: '/',
path: '/',
href: 'https://outposts.us-west-2.amazonaws.com/'
},
_events: { apiCallAttempt: [Array], apiCall: [Array] },
MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],
CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],
_clientId: 1
},
operation: 'getOutpost',
params: { OutpostId: 'op-0e32dade1930682b8' },
httpRequest: HttpRequest {
method: 'GET',
path: '/outposts/op-0e32dade1930682b8',
headers: {
'User-Agent': 'aws-sdk-nodejs/2.1151.0 linux/v17.4.0 callback',
'X-Amz-Content-Sha256': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'Content-Length': 0,
Host: 'outposts.us-west-2.amazonaws.com',
'X-Amz-Date': '20220610T082425Z',
Authorization: 'AWS4-HMAC-SHA256 Credential=AKIAWRW4PGALHIBCPTWT/20220610/us-west-2/outposts/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=44ed28c50dca07327c0bb252b7897ab43b81ec53aef26ee8c20cffb2426239f3'
},
body: '',
endpoint: {
protocol: 'https:',
host: 'outposts.us-west-2.amazonaws.com',
port: 443,
hostname: 'outposts.us-west-2.amazonaws.com',
pathname: '/',
path: '/',
href: 'https://outposts.us-west-2.amazonaws.com/',
constructor: [Function]
},
region: 'us-west-2',
_userAgent: 'aws-sdk-nodejs/2.1151.0 linux/v17.4.0 callback',
stream: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
outputData: [Array],
outputSize: 542,
writable: true,
destroyed: false,
_last: false,
chunkedEncoding: false,
shouldKeepAlive: true,
maxRequestsOnConnectionReached: false,
_defaultKeepAlive: true,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
_closed: false,
socket: null,
_header: 'GET /outposts/op-0e32dade1930682b8 HTTP/1.1\r\n' +
'User-Agent: aws-sdk-nodejs/2.1151.0 linux/v17.4.0 callback\r\n' +
'X-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\r\n' +
'Content-Length: 0\r\n' +
'Host: outposts.us-west-2.amazonaws.com\r\n' +
'X-Amz-Date: 20220610T082425Z\r\n' +
'Authorization: AWS4-HMAC-SHA256 Credential=AKIAWRW4PGALHIBCPTWT/20220610/us-west-2/outposts/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=44ed28c50dca07327c0bb252b7897ab43b81ec53aef26ee8c20cffb2426239f3\r\n' +
'Connection: keep-alive\r\n' +
'\r\n',
_keepAliveTimeout: 0,
_onPendingData: [Function: nop],
agent: [Agent],
socketPath: undefined,
method: 'GET',
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: '/outposts/op-0e32dade1930682b8',
_ended: false,
res: null,
aborted: false,
timeoutCb: [Function: emitRequestTimeout],
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: 'outposts.us-west-2.amazonaws.com',
protocol: 'https:',
didCallback: false,
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: false,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
}
},
startTime: 2022-06-10T08:24:25.019Z,
response: Response {
request: [Circular *1],
data: null,
error: null,
retryCount: 0,
redirectCount: 0,
httpResponse: HttpResponse {
statusCode: undefined,
headers: {},
body: undefined,
streaming: false,
stream: null,
_abortCallback: [Function: callNextListener]
},
maxRetries: 3,
maxRedirects: 10
},
_asm: AcceptorStateMachine {
currentState: 'send',
states: {
validate: [Object],
build: [Object],
afterBuild: [Object],
sign: [Object],
retry: [Object],
afterRetry: [Object],
send: [Object],
validateResponse: [Object],
extractError: [Object],
extractData: [Object],
restart: [Object],
success: [Object],
error: [Object],
complete: [Object]
}
},
_haltHandlersOnError: false,
_events: {
validate: [
[Function (anonymous)],
[Function],
[Function: VALIDATE_REGION],
[Function: BUILD_IDEMPOTENCY_TOKENS],
[Function: VALIDATE_PARAMETERS]
],
afterBuild: [
[Function: COMPUTE_CHECKSUM],
[Function],
[Function: SET_CONTENT_LENGTH],
[Function: SET_HTTP_HOST],
[Function: SET_TRACE_ID]
],
restart: [ [Function: RESTART] ],
sign: [ [Function (anonymous)], [Function], [Function] ],
validateResponse: [ [Function: VALIDATE_RESPONSE], [Function (anonymous)] ],
send: [ [Function] ],
httpHeaders: [ [Function: HTTP_HEADERS] ],
httpData: [ [Function: HTTP_DATA] ],
httpDone: [ [Function: HTTP_DONE] ],
retry: [
[Function: FINALIZE_ERROR],
[Function: INVALIDATE_CREDENTIALS],
[Function: EXPIRED_SIGNATURE],
[Function: CLOCK_SKEWED],
[Function: REDIRECT],
[Function: RETRY_CHECK],
[Function: API_CALL_ATTEMPT_RETRY]
],
afterRetry: [ [Function] ],
build: [ [Function: buildRequest] ],
extractData: [ [Function: extractData], [Function: extractRequestId] ],
extractError: [ [Function: extractError], [Function: extractRequestId] ],
httpError: [ [Function: ENOTFOUND_ERROR] ],
success: [ [Function: API_CALL_ATTEMPT] ],
complete: [ [Function: API_CALL], [Function (anonymous)] ]
},
emit: [Function: emit],
API_CALL_ATTEMPT: [Function: API_CALL_ATTEMPT],
API_CALL_ATTEMPT_RETRY: [Function: API_CALL_ATTEMPT_RETRY],
API_CALL: [Function: API_CALL],
signedAt: 2022-06-10T08:24:25.025Z
}
Upvotes: 0
Views: 407
Reputation: 265
In your GetPostArnData
method you can return a new Promise
and use resolve
and reject
to return the values. Try something like this:
Note: you can do a shorthand for constructors.
class Outpost {
constructor(public outpostId: string, public region: string) {
AWS.config.update({ region: this.region });
}
GetOutpostArn() {
return new Promise((resolve, reject) => {
new AWS.Outposts().getOutpost(
{ OutpostId: this.outpostId },
(err, data) => {
if (err) reject(err); // an error occurred
resolve(data.Outpost?.OutpostArn);
}
);
});
}
}
(async () => {
const outpost = new Outpost("op-0e32dade1930682b8", "us-west-2");
const data = await outpost.GetOutpostArn();
console.log(data);
})();
Upvotes: 1